@aztec/simulator 0.23.0 → 0.24.0
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/acvm/oracle/oracle.d.ts +2 -2
- package/dest/acvm/oracle/oracle.d.ts.map +1 -1
- package/dest/acvm/oracle/oracle.js +5 -5
- package/dest/acvm/oracle/typed_oracle.d.ts +2 -2
- package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
- package/dest/acvm/oracle/typed_oracle.js +3 -3
- package/dest/avm/avm_execution_environment.d.ts +3 -2
- package/dest/avm/avm_execution_environment.d.ts.map +1 -1
- package/dest/avm/avm_execution_environment.js +6 -5
- package/dest/avm/avm_memory_types.d.ts +119 -38
- package/dest/avm/avm_memory_types.d.ts.map +1 -1
- package/dest/avm/avm_memory_types.js +91 -109
- package/dest/avm/avm_simulator.d.ts.map +1 -1
- package/dest/avm/avm_simulator.js +2 -3
- package/dest/avm/errors.d.ts +3 -1
- package/dest/avm/errors.d.ts.map +1 -1
- package/dest/avm/errors.js +9 -3
- package/dest/avm/fixtures/index.d.ts +4 -0
- package/dest/avm/fixtures/index.d.ts.map +1 -1
- package/dest/avm/fixtures/index.js +10 -2
- package/dest/avm/journal/host_storage.d.ts +1 -1
- package/dest/avm/journal/host_storage.d.ts.map +1 -1
- package/dest/avm/opcodes/addressing_mode.d.ts +24 -0
- package/dest/avm/opcodes/addressing_mode.d.ts.map +1 -0
- package/dest/avm/opcodes/addressing_mode.js +62 -0
- package/dest/avm/opcodes/comparators.d.ts.map +1 -1
- package/dest/avm/opcodes/comparators.js +8 -5
- package/dest/avm/opcodes/instruction.d.ts +4 -4
- package/dest/avm/opcodes/instruction.d.ts.map +1 -1
- package/dest/avm/opcodes/instruction.js +1 -1
- package/dest/avm/opcodes/memory.d.ts.map +1 -1
- package/dest/avm/opcodes/memory.js +5 -3
- package/dest/avm/serialization/bytecode_serialization.d.ts.map +1 -1
- package/dest/avm/serialization/bytecode_serialization.js +24 -22
- package/dest/avm/serialization/instruction_serialization.d.ts +17 -15
- package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
- package/dest/avm/serialization/instruction_serialization.js +18 -16
- package/dest/avm/temporary_executor_migration.d.ts +25 -0
- package/dest/avm/temporary_executor_migration.d.ts.map +1 -0
- package/dest/avm/temporary_executor_migration.js +71 -0
- package/dest/client/client_execution_context.d.ts +4 -2
- package/dest/client/client_execution_context.d.ts.map +1 -1
- package/dest/client/client_execution_context.js +7 -4
- package/dest/client/execution_result.d.ts +2 -0
- package/dest/client/execution_result.d.ts.map +1 -1
- package/dest/client/execution_result.js +1 -1
- package/dest/client/simulator.d.ts +10 -5
- package/dest/client/simulator.d.ts.map +1 -1
- package/dest/client/simulator.js +19 -11
- package/dest/client/unconstrained_execution.js +2 -2
- package/dest/public/execution.js +2 -2
- package/dest/public/executor.d.ts +7 -0
- package/dest/public/executor.d.ts.map +1 -1
- package/dest/public/executor.js +26 -1
- package/dest/public/public_execution_context.js +2 -2
- package/dest/public/state_actions.d.ts +1 -1
- package/dest/public/state_actions.d.ts.map +1 -1
- package/dest/public/state_actions.js +5 -6
- package/dest/utils.d.ts +5 -20
- package/dest/utils.d.ts.map +1 -1
- package/dest/utils.js +4 -20
- package/package.json +7 -5
- package/src/acvm/acvm.ts +156 -0
- package/src/acvm/acvm_types.ts +11 -0
- package/src/acvm/deserialize.ts +36 -0
- package/src/acvm/index.ts +5 -0
- package/src/acvm/oracle/debug.ts +109 -0
- package/src/acvm/oracle/index.ts +17 -0
- package/src/acvm/oracle/oracle.ts +332 -0
- package/src/acvm/oracle/typed_oracle.ts +217 -0
- package/src/acvm/serialize.ts +75 -0
- package/src/avm/avm_context.ts +63 -0
- package/src/avm/avm_execution_environment.ts +98 -0
- package/src/avm/avm_machine_state.ts +93 -0
- package/src/avm/avm_memory_types.ts +309 -0
- package/src/avm/avm_message_call_result.ts +29 -0
- package/src/avm/avm_simulator.ts +89 -0
- package/src/avm/errors.ts +57 -0
- package/src/avm/fixtures/index.ts +90 -0
- package/src/avm/journal/host_storage.ts +20 -0
- package/src/avm/journal/index.ts +2 -0
- package/src/avm/journal/journal.ts +266 -0
- package/src/avm/opcodes/.eslintrc.cjs +8 -0
- package/src/avm/opcodes/accrued_substate.ts +92 -0
- package/src/avm/opcodes/addressing_mode.ts +66 -0
- package/src/avm/opcodes/arithmetic.ts +79 -0
- package/src/avm/opcodes/bitwise.ts +129 -0
- package/src/avm/opcodes/comparators.ts +72 -0
- package/src/avm/opcodes/control_flow.ts +129 -0
- package/src/avm/opcodes/environment_getters.ts +199 -0
- package/src/avm/opcodes/external_calls.ts +122 -0
- package/src/avm/opcodes/index.ts +10 -0
- package/src/avm/opcodes/instruction.ts +64 -0
- package/src/avm/opcodes/instruction_impl.ts +52 -0
- package/src/avm/opcodes/memory.ts +193 -0
- package/src/avm/opcodes/storage.ts +76 -0
- package/src/avm/serialization/buffer_cursor.ts +109 -0
- package/src/avm/serialization/bytecode_serialization.ts +172 -0
- package/src/avm/serialization/instruction_serialization.ts +167 -0
- package/src/avm/temporary_executor_migration.ts +108 -0
- package/src/client/client_execution_context.ts +472 -0
- package/src/client/db_oracle.ts +184 -0
- package/src/client/execution_note_cache.ts +90 -0
- package/src/client/execution_result.ts +89 -0
- package/src/client/index.ts +3 -0
- package/src/client/pick_notes.ts +125 -0
- package/src/client/private_execution.ts +78 -0
- package/src/client/simulator.ts +316 -0
- package/src/client/unconstrained_execution.ts +49 -0
- package/src/client/view_data_oracle.ts +243 -0
- package/src/common/errors.ts +61 -0
- package/src/common/index.ts +3 -0
- package/src/common/packed_args_cache.ts +55 -0
- package/src/common/side_effect_counter.ts +12 -0
- package/src/index.ts +3 -0
- package/src/public/db.ts +85 -0
- package/src/public/execution.ts +137 -0
- package/src/public/executor.ts +158 -0
- package/src/public/index.ts +9 -0
- package/src/public/public_execution_context.ts +217 -0
- package/src/public/state_actions.ts +100 -0
- package/src/test/utils.ts +38 -0
- package/src/utils.ts +18 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { FunctionData } from '@aztec/circuits.js';
|
|
2
|
+
import { DecodedReturn, FunctionArtifactWithDebugMetadata, decodeReturnValues } from '@aztec/foundation/abi';
|
|
3
|
+
import { AztecAddress } from '@aztec/foundation/aztec-address';
|
|
4
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
5
|
+
import { createDebugLogger } from '@aztec/foundation/log';
|
|
6
|
+
|
|
7
|
+
import { extractReturnWitness } from '../acvm/deserialize.js';
|
|
8
|
+
import { Oracle, acvm, extractCallStack, toACVMWitness } from '../acvm/index.js';
|
|
9
|
+
import { ExecutionError } from '../common/errors.js';
|
|
10
|
+
import { AcirSimulator } from './simulator.js';
|
|
11
|
+
import { ViewDataOracle } from './view_data_oracle.js';
|
|
12
|
+
|
|
13
|
+
// docs:start:execute_unconstrained_function
|
|
14
|
+
/**
|
|
15
|
+
* Execute an unconstrained function and return the decoded values.
|
|
16
|
+
*/
|
|
17
|
+
export async function executeUnconstrainedFunction(
|
|
18
|
+
oracle: ViewDataOracle,
|
|
19
|
+
artifact: FunctionArtifactWithDebugMetadata,
|
|
20
|
+
contractAddress: AztecAddress,
|
|
21
|
+
functionData: FunctionData,
|
|
22
|
+
args: Fr[],
|
|
23
|
+
log = createDebugLogger('aztec:simulator:unconstrained_execution'),
|
|
24
|
+
): Promise<DecodedReturn> {
|
|
25
|
+
const functionSelector = functionData.selector;
|
|
26
|
+
log(`Executing unconstrained function ${contractAddress}:${functionSelector}`);
|
|
27
|
+
|
|
28
|
+
const acir = Buffer.from(artifact.bytecode, 'base64');
|
|
29
|
+
const initialWitness = toACVMWitness(0, args);
|
|
30
|
+
const { partialWitness } = await acvm(
|
|
31
|
+
await AcirSimulator.getSolver(),
|
|
32
|
+
acir,
|
|
33
|
+
initialWitness,
|
|
34
|
+
new Oracle(oracle),
|
|
35
|
+
).catch((err: Error) => {
|
|
36
|
+
throw new ExecutionError(
|
|
37
|
+
err.message,
|
|
38
|
+
{
|
|
39
|
+
contractAddress,
|
|
40
|
+
functionSelector,
|
|
41
|
+
},
|
|
42
|
+
extractCallStack(err, artifact.debug),
|
|
43
|
+
{ cause: err },
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
return decodeReturnValues(artifact, extractReturnWitness(acir, partialWitness));
|
|
48
|
+
}
|
|
49
|
+
// docs:end:execute_unconstrained_function
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AuthWitness,
|
|
3
|
+
AztecNode,
|
|
4
|
+
CompleteAddress,
|
|
5
|
+
MerkleTreeId,
|
|
6
|
+
NoteStatus,
|
|
7
|
+
NullifierMembershipWitness,
|
|
8
|
+
PublicDataWitness,
|
|
9
|
+
} from '@aztec/circuit-types';
|
|
10
|
+
import { Header } from '@aztec/circuits.js';
|
|
11
|
+
import { siloNullifier } from '@aztec/circuits.js/abis';
|
|
12
|
+
import { AztecAddress } from '@aztec/foundation/aztec-address';
|
|
13
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
14
|
+
import { createDebugLogger } from '@aztec/foundation/log';
|
|
15
|
+
|
|
16
|
+
import { NoteData, TypedOracle } from '../acvm/index.js';
|
|
17
|
+
import { DBOracle } from './db_oracle.js';
|
|
18
|
+
import { pickNotes } from './pick_notes.js';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The execution context for a client view tx simulation.
|
|
22
|
+
* It only reads data from data sources. Nothing will be updated or created during this simulation.
|
|
23
|
+
*/
|
|
24
|
+
export class ViewDataOracle extends TypedOracle {
|
|
25
|
+
constructor(
|
|
26
|
+
protected readonly contractAddress: AztecAddress,
|
|
27
|
+
/** List of transient auth witnesses to be used during this simulation */
|
|
28
|
+
protected readonly authWitnesses: AuthWitness[],
|
|
29
|
+
protected readonly db: DBOracle,
|
|
30
|
+
protected readonly aztecNode: AztecNode,
|
|
31
|
+
protected log = createDebugLogger('aztec:simulator:client_view_context'),
|
|
32
|
+
) {
|
|
33
|
+
super();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Return the nullifier key pair of an account to use in a specific contract.
|
|
38
|
+
* @param account - The account address of the nullifier key.
|
|
39
|
+
*/
|
|
40
|
+
public getNullifierKeyPair(account: AztecAddress) {
|
|
41
|
+
return this.db.getNullifierKeyPair(account, this.contractAddress);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Fetches the index and sibling path of a leaf at a given block from a given tree.
|
|
46
|
+
* @param blockNumber - The block number at which to get the membership witness.
|
|
47
|
+
* @param treeId - Id of the tree to get the sibling path from.
|
|
48
|
+
* @param leafValue - The leaf value
|
|
49
|
+
* @returns The index and sibling path concatenated [index, sibling_path]
|
|
50
|
+
*/
|
|
51
|
+
public async getMembershipWitness(blockNumber: number, treeId: MerkleTreeId, leafValue: Fr): Promise<Fr[]> {
|
|
52
|
+
const index = await this.db.findLeafIndex(blockNumber, treeId, leafValue);
|
|
53
|
+
if (!index) {
|
|
54
|
+
throw new Error(`Leaf value: ${leafValue} not found in ${MerkleTreeId[treeId]}`);
|
|
55
|
+
}
|
|
56
|
+
const siblingPath = await this.db.getSiblingPath(blockNumber, treeId, index);
|
|
57
|
+
return [new Fr(index), ...siblingPath];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Fetches a sibling path at a given block and index from a tree specified by `treeId`.
|
|
62
|
+
* @param blockNumber - The block number at which to get the membership witness.
|
|
63
|
+
* @param treeId - Id of the tree to get the sibling path from.
|
|
64
|
+
* @param leafIndex - Index of the leaf to get sibling path for
|
|
65
|
+
* @returns The sibling path.
|
|
66
|
+
*/
|
|
67
|
+
public getSiblingPath(blockNumber: number, treeId: MerkleTreeId, leafIndex: Fr): Promise<Fr[]> {
|
|
68
|
+
return this.db.getSiblingPath(blockNumber, treeId, leafIndex.toBigInt());
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Returns a nullifier membership witness for a given nullifier at a given block.
|
|
73
|
+
* @param blockNumber - The block number at which to get the index.
|
|
74
|
+
* @param nullifier - Nullifier we try to find witness for.
|
|
75
|
+
* @returns The nullifier membership witness (if found).
|
|
76
|
+
*/
|
|
77
|
+
public async getNullifierMembershipWitness(
|
|
78
|
+
blockNumber: number,
|
|
79
|
+
nullifier: Fr,
|
|
80
|
+
): Promise<NullifierMembershipWitness | undefined> {
|
|
81
|
+
return await this.db.getNullifierMembershipWitness(blockNumber, nullifier);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Returns a low nullifier membership witness for a given nullifier at a given block.
|
|
86
|
+
* @param blockNumber - The block number at which to get the index.
|
|
87
|
+
* @param nullifier - Nullifier we try to find the low nullifier witness for.
|
|
88
|
+
* @returns The low nullifier membership witness (if found).
|
|
89
|
+
* @remarks Low nullifier witness can be used to perform a nullifier non-inclusion proof by leveraging the "linked
|
|
90
|
+
* list structure" of leaves and proving that a lower nullifier is pointing to a bigger next value than the nullifier
|
|
91
|
+
* we are trying to prove non-inclusion for.
|
|
92
|
+
*/
|
|
93
|
+
public async getLowNullifierMembershipWitness(
|
|
94
|
+
blockNumber: number,
|
|
95
|
+
nullifier: Fr,
|
|
96
|
+
): Promise<NullifierMembershipWitness | undefined> {
|
|
97
|
+
return await this.db.getLowNullifierMembershipWitness(blockNumber, nullifier);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Returns a public data tree witness for a given leaf slot at a given block.
|
|
102
|
+
* @param blockNumber - The block number at which to get the index.
|
|
103
|
+
* @param leafSlot - The slot of the public data tree to get the witness for.
|
|
104
|
+
* @returns - The witness
|
|
105
|
+
*/
|
|
106
|
+
public async getPublicDataTreeWitness(blockNumber: number, leafSlot: Fr): Promise<PublicDataWitness | undefined> {
|
|
107
|
+
return await this.db.getPublicDataTreeWitness(blockNumber, leafSlot);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Fetches a block header of a given block.
|
|
112
|
+
* @param blockNumber - The number of a block of which to get the block header.
|
|
113
|
+
* @returns Block extracted from a block with block number `blockNumber`.
|
|
114
|
+
*/
|
|
115
|
+
public async getHeader(blockNumber: number): Promise<Header | undefined> {
|
|
116
|
+
const block = await this.db.getBlock(blockNumber);
|
|
117
|
+
if (!block) {
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
return block.header;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Retrieve the complete address associated to a given address.
|
|
125
|
+
* @param address - Address to fetch the complete address for.
|
|
126
|
+
* @returns A complete address associated with the input address.
|
|
127
|
+
*/
|
|
128
|
+
public getCompleteAddress(address: AztecAddress): Promise<CompleteAddress> {
|
|
129
|
+
return this.db.getCompleteAddress(address);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Returns an auth witness for the given message hash. Checks on the list of transient witnesses
|
|
134
|
+
* for this transaction first, and falls back to the local database if not found.
|
|
135
|
+
* @param messageHash - Hash of the message to authenticate.
|
|
136
|
+
* @returns Authentication witness for the requested message hash.
|
|
137
|
+
*/
|
|
138
|
+
public getAuthWitness(messageHash: Fr): Promise<Fr[] | undefined> {
|
|
139
|
+
return Promise.resolve(
|
|
140
|
+
this.authWitnesses.find(w => w.requestHash.equals(messageHash))?.witness ?? this.db.getAuthWitness(messageHash),
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Pops a capsule from the capsule dispenser
|
|
146
|
+
* @returns The capsule values
|
|
147
|
+
* @remarks A capsule is a "blob" of data that is passed to the contract through an oracle.
|
|
148
|
+
*/
|
|
149
|
+
public popCapsule(): Promise<Fr[]> {
|
|
150
|
+
return this.db.popCapsule();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Gets some notes for a contract address and storage slot.
|
|
155
|
+
* Returns a flattened array containing filtered notes.
|
|
156
|
+
*
|
|
157
|
+
* @remarks
|
|
158
|
+
* Check for pending notes with matching slot.
|
|
159
|
+
* Real notes coming from DB will have a leafIndex which
|
|
160
|
+
* represents their index in the note hash tree.
|
|
161
|
+
*
|
|
162
|
+
* @param storageSlot - The storage slot.
|
|
163
|
+
* @param numSelects - The number of valid selects in selectBy and selectValues.
|
|
164
|
+
* @param selectBy - An array of indices of the fields to selects.
|
|
165
|
+
* @param selectValues - The values to match.
|
|
166
|
+
* @param selectComparators - The comparators to use to match values.
|
|
167
|
+
* @param sortBy - An array of indices of the fields to sort.
|
|
168
|
+
* @param sortOrder - The order of the corresponding index in sortBy. (1: DESC, 2: ASC, 0: Do nothing)
|
|
169
|
+
* @param limit - The number of notes to retrieve per query.
|
|
170
|
+
* @param offset - The starting index for pagination.
|
|
171
|
+
* @param status - The status of notes to fetch.
|
|
172
|
+
* @returns Array of note data.
|
|
173
|
+
*/
|
|
174
|
+
public async getNotes(
|
|
175
|
+
storageSlot: Fr,
|
|
176
|
+
numSelects: number,
|
|
177
|
+
selectBy: number[],
|
|
178
|
+
selectValues: Fr[],
|
|
179
|
+
selectComparators: number[],
|
|
180
|
+
sortBy: number[],
|
|
181
|
+
sortOrder: number[],
|
|
182
|
+
limit: number,
|
|
183
|
+
offset: number,
|
|
184
|
+
status: NoteStatus,
|
|
185
|
+
): Promise<NoteData[]> {
|
|
186
|
+
const dbNotes = await this.db.getNotes(this.contractAddress, storageSlot, status);
|
|
187
|
+
return pickNotes<NoteData>(dbNotes, {
|
|
188
|
+
selects: selectBy
|
|
189
|
+
.slice(0, numSelects)
|
|
190
|
+
.map((index, i) => ({ index, value: selectValues[i], comparator: selectComparators[i] })),
|
|
191
|
+
sorts: sortBy.map((index, i) => ({ index, order: sortOrder[i] })),
|
|
192
|
+
limit,
|
|
193
|
+
offset,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Check if a nullifier exists in the nullifier tree.
|
|
199
|
+
* @param innerNullifier - The inner nullifier.
|
|
200
|
+
* @returns A boolean indicating whether the nullifier exists in the tree or not.
|
|
201
|
+
*/
|
|
202
|
+
public async checkNullifierExists(innerNullifier: Fr) {
|
|
203
|
+
const nullifier = siloNullifier(this.contractAddress, innerNullifier!);
|
|
204
|
+
const index = await this.db.getNullifierIndex(nullifier);
|
|
205
|
+
return index !== undefined;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Fetches the a message from the db, given its key.
|
|
210
|
+
* @param msgKey - A buffer representing the message key.
|
|
211
|
+
* @returns The l1 to l2 message data
|
|
212
|
+
*/
|
|
213
|
+
public async getL1ToL2Message(msgKey: Fr) {
|
|
214
|
+
return await this.db.getL1ToL2Message(msgKey);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Retrieves the portal contract address associated with the given contract address.
|
|
219
|
+
* Throws an error if the input contract address is not found or invalid.
|
|
220
|
+
* @param contractAddress - The address of the contract whose portal address is to be fetched.
|
|
221
|
+
* @returns The portal contract address.
|
|
222
|
+
*/
|
|
223
|
+
public getPortalContractAddress(contractAddress: AztecAddress) {
|
|
224
|
+
return this.db.getPortalContractAddress(contractAddress);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Read the public storage data.
|
|
229
|
+
* @param startStorageSlot - The starting storage slot.
|
|
230
|
+
* @param numberOfElements - Number of elements to read from the starting storage slot.
|
|
231
|
+
*/
|
|
232
|
+
public async storageRead(startStorageSlot: Fr, numberOfElements: number) {
|
|
233
|
+
const values = [];
|
|
234
|
+
for (let i = 0n; i < numberOfElements; i++) {
|
|
235
|
+
const storageSlot = new Fr(startStorageSlot.value + i);
|
|
236
|
+
const value = await this.aztecNode.getPublicStorageAt(this.contractAddress, storageSlot);
|
|
237
|
+
|
|
238
|
+
this.log(`Oracle storage read: slot=${storageSlot.toString()} value=${value}`);
|
|
239
|
+
values.push(value);
|
|
240
|
+
}
|
|
241
|
+
return values;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { FailingFunction, NoirCallStack, SimulationError } from '@aztec/circuit-types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* An error that occurred during the execution of a function.
|
|
5
|
+
*/
|
|
6
|
+
export class ExecutionError extends Error {
|
|
7
|
+
constructor(
|
|
8
|
+
message: string,
|
|
9
|
+
/**
|
|
10
|
+
* The function that failed.
|
|
11
|
+
*/
|
|
12
|
+
public failingFunction: FailingFunction,
|
|
13
|
+
/**
|
|
14
|
+
* The noir call stack of the function that failed.
|
|
15
|
+
*/
|
|
16
|
+
public noirCallStack?: NoirCallStack,
|
|
17
|
+
options?: ErrorOptions,
|
|
18
|
+
) {
|
|
19
|
+
super(message, options);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Traverses the cause chain of an error.
|
|
25
|
+
* @param error - The error to start from.
|
|
26
|
+
* @param callback - A callback on every error, including the first one.
|
|
27
|
+
*/
|
|
28
|
+
export function traverseCauseChain(error: Error, callback: (error: Error) => void) {
|
|
29
|
+
let currentError: Error | undefined = error;
|
|
30
|
+
while (currentError) {
|
|
31
|
+
callback(currentError);
|
|
32
|
+
if (currentError.cause && currentError.cause instanceof Error) {
|
|
33
|
+
currentError = currentError.cause;
|
|
34
|
+
} else {
|
|
35
|
+
currentError = undefined;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Creates a simulation error from an error chain generated during the execution of a function.
|
|
42
|
+
* @param error - The error thrown during execution.
|
|
43
|
+
* @returns - A simulation error.
|
|
44
|
+
*/
|
|
45
|
+
export function createSimulationError(error: Error): SimulationError {
|
|
46
|
+
let rootCause = error;
|
|
47
|
+
let noirCallStack: NoirCallStack | undefined = undefined;
|
|
48
|
+
const aztecCallStack: FailingFunction[] = [];
|
|
49
|
+
|
|
50
|
+
traverseCauseChain(error, cause => {
|
|
51
|
+
rootCause = cause;
|
|
52
|
+
if (cause instanceof ExecutionError) {
|
|
53
|
+
aztecCallStack.push(cause.failingFunction);
|
|
54
|
+
if (cause.noirCallStack) {
|
|
55
|
+
noirCallStack = cause.noirCallStack;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return new SimulationError(rootCause.message, aztecCallStack, noirCallStack, { cause: rootCause });
|
|
61
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { PackedArguments } from '@aztec/circuit-types';
|
|
2
|
+
import { Fr } from '@aztec/circuits.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A cache for packed arguments during transaction execution.
|
|
6
|
+
*/
|
|
7
|
+
export class PackedArgsCache {
|
|
8
|
+
private cache: Map<bigint, Fr[]>;
|
|
9
|
+
|
|
10
|
+
constructor(initialArguments: PackedArguments[] = []) {
|
|
11
|
+
this.cache = new Map();
|
|
12
|
+
for (const initialArg of initialArguments) {
|
|
13
|
+
this.cache.set(initialArg.hash.toBigInt(), initialArg.args);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new packed arguments cache.
|
|
19
|
+
* @param initialArguments - The initial arguments to add to the cache.
|
|
20
|
+
* @returns The new packed arguments cache.
|
|
21
|
+
*/
|
|
22
|
+
public static create(initialArguments: PackedArguments[] = []) {
|
|
23
|
+
return new PackedArgsCache(initialArguments);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Unpacks packed arguments.
|
|
28
|
+
* @param hash - The hash of the packed arguments.
|
|
29
|
+
* @returns The unpacked arguments.
|
|
30
|
+
*/
|
|
31
|
+
public unpack(hash: Fr): Fr[] {
|
|
32
|
+
if (hash.equals(Fr.ZERO)) {
|
|
33
|
+
return [];
|
|
34
|
+
}
|
|
35
|
+
const packedArgs = this.cache.get(hash.value);
|
|
36
|
+
if (!packedArgs) {
|
|
37
|
+
throw new Error(`Packed arguments for hash ${hash.toString()} not found in cache`);
|
|
38
|
+
}
|
|
39
|
+
return packedArgs;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Packs arguments.
|
|
44
|
+
* @param args - The arguments to pack.
|
|
45
|
+
* @returns The hash of the packed arguments.
|
|
46
|
+
*/
|
|
47
|
+
public pack(args: Fr[]) {
|
|
48
|
+
if (args.length === 0) {
|
|
49
|
+
return Fr.ZERO;
|
|
50
|
+
}
|
|
51
|
+
const packedArguments = PackedArguments.fromArgs(args);
|
|
52
|
+
this.cache.set(packedArguments.hash.value, packedArguments.args);
|
|
53
|
+
return packedArguments.hash;
|
|
54
|
+
}
|
|
55
|
+
}
|
package/src/index.ts
ADDED
package/src/public/db.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { EthAddress, FunctionSelector, L1_TO_L2_MSG_TREE_HEIGHT } from '@aztec/circuits.js';
|
|
2
|
+
import { AztecAddress } from '@aztec/foundation/aztec-address';
|
|
3
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
4
|
+
|
|
5
|
+
import { MessageLoadOracleInputs } from '../acvm/index.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Database interface for providing access to public state.
|
|
9
|
+
*/
|
|
10
|
+
export interface PublicStateDB {
|
|
11
|
+
/**
|
|
12
|
+
* Reads a value from public storage, returning zero if none.
|
|
13
|
+
* @param contract - Owner of the storage.
|
|
14
|
+
* @param slot - Slot to read in the contract storage.
|
|
15
|
+
* @returns The current value in the storage slot.
|
|
16
|
+
*/
|
|
17
|
+
storageRead(contract: AztecAddress, slot: Fr): Promise<Fr>;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Records a write to public storage.
|
|
21
|
+
* @param contract - Owner of the storage.
|
|
22
|
+
* @param slot - Slot to read in the contract storage.
|
|
23
|
+
* @param newValue - The new value to store.
|
|
24
|
+
* @returns Nothing.
|
|
25
|
+
*/
|
|
26
|
+
storageWrite(contract: AztecAddress, slot: Fr, newValue: Fr): Promise<void>;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Commit the pending changes to the DB.
|
|
30
|
+
* @returns Nothing.
|
|
31
|
+
*/
|
|
32
|
+
commit(): Promise<void>;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Rollback the pending changes.
|
|
36
|
+
* @returns Nothing.
|
|
37
|
+
*/
|
|
38
|
+
rollback(): Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Database interface for providing access to public contract data.
|
|
43
|
+
*/
|
|
44
|
+
export interface PublicContractsDB {
|
|
45
|
+
/**
|
|
46
|
+
* Returns the brillig (public bytecode) of a function.
|
|
47
|
+
* @param address - The contract address that owns this function.
|
|
48
|
+
* @param selector - The selector for the function.
|
|
49
|
+
* @returns The bytecode or undefined if not found.
|
|
50
|
+
*/
|
|
51
|
+
getBytecode(address: AztecAddress, selector: FunctionSelector): Promise<Buffer | undefined>;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Returns whether a function is internal or not.
|
|
55
|
+
* @param address - The contract address that owns this function.
|
|
56
|
+
* @param selector - The selector for the function.
|
|
57
|
+
* @returns The `isInternal` flag found, undefined if not found.
|
|
58
|
+
*/
|
|
59
|
+
getIsInternal(address: AztecAddress, selector: FunctionSelector): Promise<boolean | undefined>;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Returns the portal contract address for an L2 address.
|
|
63
|
+
* @param address - The L2 contract address.
|
|
64
|
+
* @returns The portal contract address or undefined if not found.
|
|
65
|
+
*/
|
|
66
|
+
getPortalContractAddress(address: AztecAddress): Promise<EthAddress | undefined>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Database interface for providing access to commitment tree and l1 to l2 message tree (append only data trees). */
|
|
70
|
+
export interface CommitmentsDB {
|
|
71
|
+
/**
|
|
72
|
+
* Gets a confirmed L1 to L2 message for the given message key.
|
|
73
|
+
* TODO(Maddiaa): Can be combined with aztec-node method that does the same thing.
|
|
74
|
+
* @param msgKey - The message Key.
|
|
75
|
+
* @returns - The l1 to l2 message object
|
|
76
|
+
*/
|
|
77
|
+
getL1ToL2Message(msgKey: Fr): Promise<MessageLoadOracleInputs<typeof L1_TO_L2_MSG_TREE_HEIGHT>>;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Gets the index of a commitment in the note hash tree.
|
|
81
|
+
* @param commitment - The commitment.
|
|
82
|
+
* @returns - The index of the commitment. Undefined if it does not exist in the tree.
|
|
83
|
+
*/
|
|
84
|
+
getCommitmentIndex(commitment: Fr): Promise<bigint | undefined>;
|
|
85
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { FunctionL2Logs } from '@aztec/circuit-types';
|
|
2
|
+
import {
|
|
3
|
+
AztecAddress,
|
|
4
|
+
CallContext,
|
|
5
|
+
ContractStorageRead,
|
|
6
|
+
ContractStorageUpdateRequest,
|
|
7
|
+
Fr,
|
|
8
|
+
FunctionData,
|
|
9
|
+
PublicDataRead,
|
|
10
|
+
PublicDataUpdateRequest,
|
|
11
|
+
SideEffect,
|
|
12
|
+
SideEffectLinkedToNoteHash,
|
|
13
|
+
} from '@aztec/circuits.js';
|
|
14
|
+
import { computePublicDataTreeLeafSlot, computePublicDataTreeValue } from '@aztec/circuits.js/abis';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The public function execution result.
|
|
18
|
+
*/
|
|
19
|
+
export interface PublicExecutionResult {
|
|
20
|
+
/** The execution that triggered this result. */
|
|
21
|
+
execution: PublicExecution;
|
|
22
|
+
/** The return values of the function. */
|
|
23
|
+
returnValues: Fr[];
|
|
24
|
+
/** The new commitments to be inserted into the commitments tree. */
|
|
25
|
+
newCommitments: SideEffect[];
|
|
26
|
+
/** The new l2 to l1 messages generated in this call. */
|
|
27
|
+
newL2ToL1Messages: Fr[];
|
|
28
|
+
/** The new nullifiers to be inserted into the nullifier tree. */
|
|
29
|
+
newNullifiers: SideEffectLinkedToNoteHash[];
|
|
30
|
+
/** The contract storage reads performed by the function. */
|
|
31
|
+
contractStorageReads: ContractStorageRead[];
|
|
32
|
+
/** The contract storage update requests performed by the function. */
|
|
33
|
+
contractStorageUpdateRequests: ContractStorageUpdateRequest[];
|
|
34
|
+
/** The results of nested calls. */
|
|
35
|
+
nestedExecutions: this[];
|
|
36
|
+
/**
|
|
37
|
+
* Unencrypted logs emitted during execution of this function call.
|
|
38
|
+
* Note: These are preimages to `unencryptedLogsHash`.
|
|
39
|
+
*/
|
|
40
|
+
unencryptedLogs: FunctionL2Logs;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The execution of a public function.
|
|
45
|
+
*/
|
|
46
|
+
export interface PublicExecution {
|
|
47
|
+
/** Address of the contract being executed. */
|
|
48
|
+
contractAddress: AztecAddress;
|
|
49
|
+
/** Function of the contract being called. */
|
|
50
|
+
functionData: FunctionData;
|
|
51
|
+
/** Arguments for the call. */
|
|
52
|
+
args: Fr[];
|
|
53
|
+
/** Context of the call. */
|
|
54
|
+
callContext: CallContext;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Returns if the input is a public execution result and not just a public execution.
|
|
59
|
+
* @param input - Public execution or public execution result.
|
|
60
|
+
* @returns Whether the input is a public execution result and not just a public execution.
|
|
61
|
+
*/
|
|
62
|
+
export function isPublicExecutionResult(
|
|
63
|
+
input: PublicExecution | PublicExecutionResult,
|
|
64
|
+
): input is PublicExecutionResult {
|
|
65
|
+
return !!(input as PublicExecutionResult).execution;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Collect all public storage reads across all nested executions
|
|
70
|
+
* and convert them to PublicDataReads (to match kernel output).
|
|
71
|
+
* @param execResult - The topmost execution result.
|
|
72
|
+
* @returns All public data reads (in execution order).
|
|
73
|
+
*/
|
|
74
|
+
export function collectPublicDataReads(execResult: PublicExecutionResult): PublicDataRead[] {
|
|
75
|
+
// HACK(#1622): part of temporary hack - may be able to remove this function after public state ordering is fixed
|
|
76
|
+
const contractAddress = execResult.execution.contractAddress;
|
|
77
|
+
|
|
78
|
+
const thisExecPublicDataReads = execResult.contractStorageReads.map(read =>
|
|
79
|
+
contractStorageReadToPublicDataRead(read, contractAddress),
|
|
80
|
+
);
|
|
81
|
+
const unsorted = [
|
|
82
|
+
...thisExecPublicDataReads,
|
|
83
|
+
...[...execResult.nestedExecutions].flatMap(result => collectPublicDataReads(result)),
|
|
84
|
+
];
|
|
85
|
+
return unsorted.sort((a, b) => a.sideEffectCounter! - b.sideEffectCounter!);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Collect all public storage update requests across all nested executions
|
|
90
|
+
* and convert them to PublicDataUpdateRequests (to match kernel output).
|
|
91
|
+
* @param execResult - The topmost execution result.
|
|
92
|
+
* @returns All public data reads (in execution order).
|
|
93
|
+
*/
|
|
94
|
+
export function collectPublicDataUpdateRequests(execResult: PublicExecutionResult): PublicDataUpdateRequest[] {
|
|
95
|
+
// HACK(#1622): part of temporary hack - may be able to remove this function after public state ordering is fixed
|
|
96
|
+
const contractAddress = execResult.execution.contractAddress;
|
|
97
|
+
|
|
98
|
+
const thisExecPublicDataUpdateRequests = execResult.contractStorageUpdateRequests.map(update =>
|
|
99
|
+
contractStorageUpdateRequestToPublicDataUpdateRequest(update, contractAddress),
|
|
100
|
+
);
|
|
101
|
+
const unsorted = [
|
|
102
|
+
...thisExecPublicDataUpdateRequests,
|
|
103
|
+
...[...execResult.nestedExecutions].flatMap(result => collectPublicDataUpdateRequests(result)),
|
|
104
|
+
];
|
|
105
|
+
return unsorted.sort((a, b) => a.sideEffectCounter! - b.sideEffectCounter!);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Convert a Contract Storage Read to a Public Data Read.
|
|
110
|
+
* @param read - the contract storage read to convert
|
|
111
|
+
* @param contractAddress - the contract address of the read
|
|
112
|
+
* @returns The public data read.
|
|
113
|
+
*/
|
|
114
|
+
function contractStorageReadToPublicDataRead(read: ContractStorageRead, contractAddress: AztecAddress): PublicDataRead {
|
|
115
|
+
return new PublicDataRead(
|
|
116
|
+
computePublicDataTreeLeafSlot(contractAddress, read.storageSlot),
|
|
117
|
+
computePublicDataTreeValue(read.currentValue),
|
|
118
|
+
read.sideEffectCounter!,
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Convert a Contract Storage Update Request to a Public Data Update Request.
|
|
124
|
+
* @param update - the contract storage update request to convert
|
|
125
|
+
* @param contractAddress - the contract address of the data update request.
|
|
126
|
+
* @returns The public data update request.
|
|
127
|
+
*/
|
|
128
|
+
function contractStorageUpdateRequestToPublicDataUpdateRequest(
|
|
129
|
+
update: ContractStorageUpdateRequest,
|
|
130
|
+
contractAddress: AztecAddress,
|
|
131
|
+
): PublicDataUpdateRequest {
|
|
132
|
+
return new PublicDataUpdateRequest(
|
|
133
|
+
computePublicDataTreeLeafSlot(contractAddress, update.storageSlot),
|
|
134
|
+
computePublicDataTreeValue(update.newValue),
|
|
135
|
+
update.sideEffectCounter!,
|
|
136
|
+
);
|
|
137
|
+
}
|