@aztec/txe 0.0.0-test.1 → 0.0.1-commit.023c3e5
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/bin/index.d.ts +1 -1
- package/dest/bin/index.js +3 -2
- package/dest/constants.d.ts +3 -0
- package/dest/constants.d.ts.map +1 -0
- package/dest/constants.js +2 -0
- package/dest/index.d.ts +1 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +81 -51
- package/dest/oracle/interfaces.d.ts +57 -0
- package/dest/oracle/interfaces.d.ts.map +1 -0
- package/dest/oracle/interfaces.js +3 -0
- package/dest/oracle/txe_oracle_public_context.d.ts +34 -0
- package/dest/oracle/txe_oracle_public_context.d.ts.map +1 -0
- package/dest/oracle/txe_oracle_public_context.js +122 -0
- package/dest/oracle/txe_oracle_top_level_context.d.ts +66 -0
- package/dest/oracle/txe_oracle_top_level_context.d.ts.map +1 -0
- package/dest/oracle/txe_oracle_top_level_context.js +471 -0
- package/dest/rpc_translator.d.ts +252 -0
- package/dest/rpc_translator.d.ts.map +1 -0
- package/dest/rpc_translator.js +679 -0
- package/dest/state_machine/archiver.d.ts +31 -0
- package/dest/state_machine/archiver.d.ts.map +1 -0
- package/dest/state_machine/archiver.js +85 -0
- package/dest/state_machine/dummy_p2p_client.d.ts +55 -0
- package/dest/state_machine/dummy_p2p_client.d.ts.map +1 -0
- package/dest/state_machine/dummy_p2p_client.js +141 -0
- package/dest/state_machine/global_variable_builder.d.ts +11 -0
- package/dest/state_machine/global_variable_builder.d.ts.map +1 -0
- package/dest/state_machine/global_variable_builder.js +22 -0
- package/dest/state_machine/index.d.ts +16 -0
- package/dest/state_machine/index.d.ts.map +1 -0
- package/dest/state_machine/index.js +63 -0
- package/dest/state_machine/mock_epoch_cache.d.ts +27 -0
- package/dest/state_machine/mock_epoch_cache.d.ts.map +1 -0
- package/dest/state_machine/mock_epoch_cache.js +54 -0
- package/dest/state_machine/synchronizer.d.ts +33 -0
- package/dest/state_machine/synchronizer.d.ts.map +1 -0
- package/dest/state_machine/synchronizer.js +59 -0
- package/dest/txe_session.d.ts +75 -0
- package/dest/txe_session.d.ts.map +1 -0
- package/dest/txe_session.js +302 -0
- package/dest/util/encoding.d.ts +641 -24
- package/dest/util/encoding.d.ts.map +1 -1
- package/dest/util/encoding.js +66 -11
- package/dest/util/expected_failure_error.d.ts +1 -1
- package/dest/util/expected_failure_error.d.ts.map +1 -1
- package/dest/util/txe_account_store.d.ts +10 -0
- package/dest/util/txe_account_store.d.ts.map +1 -0
- package/dest/util/{txe_account_data_provider.js → txe_account_store.js} +1 -1
- package/dest/util/txe_contract_store.d.ts +12 -0
- package/dest/util/txe_contract_store.d.ts.map +1 -0
- package/dest/util/txe_contract_store.js +22 -0
- package/dest/util/txe_public_contract_data_source.d.ts +12 -11
- package/dest/util/txe_public_contract_data_source.d.ts.map +1 -1
- package/dest/util/txe_public_contract_data_source.js +37 -58
- package/dest/utils/block_creation.d.ts +28 -0
- package/dest/utils/block_creation.d.ts.map +1 -0
- package/dest/utils/block_creation.js +56 -0
- package/dest/utils/tx_effect_creation.d.ts +5 -0
- package/dest/utils/tx_effect_creation.d.ts.map +1 -0
- package/dest/utils/tx_effect_creation.js +13 -0
- package/package.json +30 -24
- package/src/bin/index.ts +3 -2
- package/src/constants.ts +3 -0
- package/src/index.ts +105 -74
- package/src/oracle/interfaces.ts +86 -0
- package/src/oracle/txe_oracle_public_context.ts +158 -0
- package/src/oracle/txe_oracle_top_level_context.ts +744 -0
- package/src/rpc_translator.ts +1098 -0
- package/src/state_machine/archiver.ts +95 -0
- package/src/state_machine/dummy_p2p_client.ts +209 -0
- package/src/state_machine/global_variable_builder.ts +38 -0
- package/src/state_machine/index.ts +100 -0
- package/src/state_machine/mock_epoch_cache.ts +67 -0
- package/src/state_machine/synchronizer.ts +88 -0
- package/src/txe_session.ts +539 -0
- package/src/util/encoding.ts +85 -11
- package/src/util/{txe_account_data_provider.ts → txe_account_store.ts} +1 -1
- package/src/util/txe_contract_store.ts +36 -0
- package/src/util/txe_public_contract_data_source.ts +43 -58
- package/src/utils/block_creation.ts +95 -0
- package/src/utils/tx_effect_creation.ts +30 -0
- package/dest/node/txe_node.d.ts +0 -358
- package/dest/node/txe_node.d.ts.map +0 -1
- package/dest/node/txe_node.js +0 -504
- package/dest/oracle/txe_oracle.d.ts +0 -152
- package/dest/oracle/txe_oracle.d.ts.map +0 -1
- package/dest/oracle/txe_oracle.js +0 -833
- package/dest/txe_service/txe_service.d.ts +0 -212
- package/dest/txe_service/txe_service.d.ts.map +0 -1
- package/dest/txe_service/txe_service.js +0 -572
- package/dest/util/txe_account_data_provider.d.ts +0 -10
- package/dest/util/txe_account_data_provider.d.ts.map +0 -1
- package/dest/util/txe_world_state_db.d.ts +0 -14
- package/dest/util/txe_world_state_db.d.ts.map +0 -1
- package/dest/util/txe_world_state_db.js +0 -27
- package/src/node/txe_node.ts +0 -725
- package/src/oracle/txe_oracle.ts +0 -1241
- package/src/txe_service/txe_service.ts +0 -749
- package/src/util/txe_world_state_db.ts +0 -38
package/src/index.ts
CHANGED
|
@@ -1,25 +1,27 @@
|
|
|
1
1
|
import { SchnorrAccountContractArtifact } from '@aztec/accounts/schnorr';
|
|
2
|
+
import { type NoirCompiledContract, loadContractArtifact } from '@aztec/aztec.js/abi';
|
|
3
|
+
import { AztecAddress } from '@aztec/aztec.js/addresses';
|
|
2
4
|
import {
|
|
3
|
-
AztecAddress,
|
|
4
|
-
type ContractArtifact,
|
|
5
5
|
type ContractInstanceWithAddress,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
loadContractArtifact,
|
|
11
|
-
} from '@aztec/aztec.js';
|
|
6
|
+
getContractInstanceFromInstantiationParams,
|
|
7
|
+
} from '@aztec/aztec.js/contracts';
|
|
8
|
+
import { Fr } from '@aztec/aztec.js/fields';
|
|
9
|
+
import { PublicKeys, deriveKeys } from '@aztec/aztec.js/keys';
|
|
12
10
|
import { createSafeJsonRpcServer } from '@aztec/foundation/json-rpc/server';
|
|
13
11
|
import type { Logger } from '@aztec/foundation/log';
|
|
14
12
|
import { type ProtocolContract, protocolContractNames } from '@aztec/protocol-contracts';
|
|
15
13
|
import { BundledProtocolContractsProvider } from '@aztec/protocol-contracts/providers/bundle';
|
|
16
|
-
import
|
|
14
|
+
import { computeArtifactHash } from '@aztec/stdlib/contract';
|
|
15
|
+
import type { ApiSchemaFor } from '@aztec/stdlib/schemas';
|
|
16
|
+
import { zodFor } from '@aztec/stdlib/schemas';
|
|
17
17
|
|
|
18
|
+
import { createHash } from 'crypto';
|
|
19
|
+
import { createReadStream } from 'fs';
|
|
18
20
|
import { readFile, readdir } from 'fs/promises';
|
|
19
|
-
import { join } from 'path';
|
|
21
|
+
import { join, parse } from 'path';
|
|
20
22
|
import { z } from 'zod';
|
|
21
23
|
|
|
22
|
-
import {
|
|
24
|
+
import { type TXEOracleFunctionName, TXESession } from './txe_session.js';
|
|
23
25
|
import {
|
|
24
26
|
type ForeignCallArgs,
|
|
25
27
|
ForeignCallArgsSchema,
|
|
@@ -29,94 +31,125 @@ import {
|
|
|
29
31
|
type ForeignCallSingle,
|
|
30
32
|
fromArray,
|
|
31
33
|
fromSingle,
|
|
32
|
-
toForeignCallResult,
|
|
33
34
|
toSingle,
|
|
34
35
|
} from './util/encoding.js';
|
|
36
|
+
import type { ContractArtifactWithHash } from './util/txe_contract_store.js';
|
|
35
37
|
|
|
36
|
-
const
|
|
38
|
+
const sessions = new Map<number, TXESession>();
|
|
37
39
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
/*
|
|
41
|
+
* TXE typically has to load the same contract artifacts over and over again for multiple tests,
|
|
42
|
+
* so we cache them here to avoid both loading them from disk repeatedly and computing their artifact hashes
|
|
43
|
+
*/
|
|
44
|
+
const TXEArtifactsCache = new Map<
|
|
45
|
+
string,
|
|
46
|
+
{ artifact: ContractArtifactWithHash; instance: ContractInstanceWithAddress }
|
|
47
|
+
>();
|
|
43
48
|
|
|
44
49
|
type TXEForeignCallInput = {
|
|
45
50
|
session_id: number;
|
|
46
|
-
function:
|
|
51
|
+
function: TXEOracleFunctionName;
|
|
47
52
|
root_path: string;
|
|
48
53
|
package_name: string;
|
|
49
54
|
inputs: ForeignCallArgs;
|
|
50
55
|
};
|
|
51
56
|
|
|
52
|
-
const TXEForeignCallInputSchema =
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
57
|
+
const TXEForeignCallInputSchema = zodFor<TXEForeignCallInput>()(
|
|
58
|
+
z.object({
|
|
59
|
+
// eslint-disable-next-line camelcase
|
|
60
|
+
session_id: z.number().int().nonnegative(),
|
|
61
|
+
function: z.string() as z.ZodType<TXEOracleFunctionName>,
|
|
62
|
+
// eslint-disable-next-line camelcase
|
|
63
|
+
root_path: z.string(),
|
|
64
|
+
// eslint-disable-next-line camelcase
|
|
65
|
+
package_name: z.string(),
|
|
66
|
+
inputs: ForeignCallArgsSchema,
|
|
67
|
+
}),
|
|
68
|
+
);
|
|
62
69
|
|
|
63
70
|
class TXEDispatcher {
|
|
64
71
|
private protocolContracts!: ProtocolContract[];
|
|
65
72
|
|
|
66
73
|
constructor(private logger: Logger) {}
|
|
67
74
|
|
|
75
|
+
private fastHashFile(path: string) {
|
|
76
|
+
return new Promise(resolve => {
|
|
77
|
+
const fd = createReadStream(path);
|
|
78
|
+
const hash = createHash('sha1');
|
|
79
|
+
hash.setEncoding('hex');
|
|
80
|
+
|
|
81
|
+
fd.on('end', function () {
|
|
82
|
+
hash.end();
|
|
83
|
+
resolve(hash.read());
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
fd.pipe(hash);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
68
90
|
async #processDeployInputs({ inputs, root_path: rootPath, package_name: packageName }: TXEForeignCallInput) {
|
|
69
|
-
const [
|
|
91
|
+
const [contractPath, initializer] = inputs.slice(0, 2).map(input =>
|
|
70
92
|
fromArray(input as ForeignCallArray)
|
|
71
93
|
.map(char => String.fromCharCode(char.toNumber()))
|
|
72
94
|
.join(''),
|
|
73
95
|
);
|
|
74
96
|
|
|
75
|
-
const decodedArgs = fromArray(inputs[
|
|
76
|
-
const secret = fromSingle(inputs[
|
|
97
|
+
const decodedArgs = fromArray(inputs[3] as ForeignCallArray);
|
|
98
|
+
const secret = fromSingle(inputs[4] as ForeignCallSingle);
|
|
77
99
|
const publicKeys = secret.equals(Fr.ZERO) ? PublicKeys.default() : (await deriveKeys(secret)).publicKeys;
|
|
78
100
|
const publicKeysHash = await publicKeys.hash();
|
|
79
101
|
|
|
80
|
-
|
|
102
|
+
let artifactPath = '';
|
|
103
|
+
const { dir: contractDirectory, base: contractFilename } = parse(contractPath);
|
|
104
|
+
if (contractDirectory) {
|
|
105
|
+
if (contractDirectory.includes('@')) {
|
|
106
|
+
// We're deploying a contract that belongs in a workspace
|
|
107
|
+
// env.deploy("../path/to/workspace/root@packageName/contractName")
|
|
108
|
+
const [workspace, pkg] = contractDirectory.split('@');
|
|
109
|
+
const targetPath = join(rootPath, workspace, '/target');
|
|
110
|
+
this.logger.debug(`Looking for compiled artifact in workspace ${targetPath}`);
|
|
111
|
+
artifactPath = join(targetPath, `${pkg}-${contractFilename}.json`);
|
|
112
|
+
} else {
|
|
113
|
+
// We're deploying a standalone external contract
|
|
114
|
+
// env.deploy("../path/to/contract/root/contractName")
|
|
115
|
+
const targetPath = join(rootPath, contractDirectory, '/target');
|
|
116
|
+
this.logger.debug(`Looking for compiled artifact in ${targetPath}`);
|
|
117
|
+
[artifactPath] = (await readdir(targetPath)).filter(file => file.endsWith(`-${contractFilename}.json`));
|
|
118
|
+
}
|
|
119
|
+
} else {
|
|
120
|
+
// We're deploying a local contract
|
|
121
|
+
// env.deploy("contractName")
|
|
122
|
+
artifactPath = join(rootPath, './target', `${packageName}-${contractFilename}.json`);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const fileHash = await this.fastHashFile(artifactPath);
|
|
126
|
+
|
|
127
|
+
const cacheKey = `${contractDirectory ?? ''}-${contractFilename}-${initializer}-${decodedArgs
|
|
81
128
|
.map(arg => arg.toString())
|
|
82
|
-
.join('-')}-${publicKeysHash
|
|
129
|
+
.join('-')}-${publicKeysHash}-${fileHash}`;
|
|
83
130
|
|
|
84
|
-
let artifact;
|
|
85
131
|
let instance;
|
|
132
|
+
let artifact: ContractArtifactWithHash;
|
|
86
133
|
|
|
87
134
|
if (TXEArtifactsCache.has(cacheKey)) {
|
|
88
135
|
this.logger.debug(`Using cached artifact for ${cacheKey}`);
|
|
89
136
|
({ artifact, instance } = TXEArtifactsCache.get(cacheKey)!);
|
|
90
137
|
} else {
|
|
91
|
-
let artifactPath = '';
|
|
92
|
-
// We're deploying the contract under test
|
|
93
|
-
// env.deploy_self("contractName")
|
|
94
|
-
if (!pathStr) {
|
|
95
|
-
artifactPath = join(rootPath, './target', `${packageName}-${contractName}.json`);
|
|
96
|
-
} else {
|
|
97
|
-
// We're deploying a contract that belongs in a workspace
|
|
98
|
-
// env.deploy("../path/to/workspace/root@packageName", "contractName")
|
|
99
|
-
if (pathStr.includes('@')) {
|
|
100
|
-
const [workspace, pkg] = pathStr.split('@');
|
|
101
|
-
const targetPath = join(rootPath, workspace, './target');
|
|
102
|
-
this.logger.debug(`Looking for compiled artifact in workspace ${targetPath}`);
|
|
103
|
-
artifactPath = join(targetPath, `${pkg}-${contractName}.json`);
|
|
104
|
-
} else {
|
|
105
|
-
// We're deploying a standalone contract
|
|
106
|
-
// env.deploy("../path/to/contract/root", "contractName")
|
|
107
|
-
const targetPath = join(rootPath, pathStr, './target');
|
|
108
|
-
this.logger.debug(`Looking for compiled artifact in ${targetPath}`);
|
|
109
|
-
[artifactPath] = (await readdir(targetPath)).filter(file => file.endsWith(`-${contractName}.json`));
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
138
|
this.logger.debug(`Loading compiled artifact ${artifactPath}`);
|
|
113
|
-
|
|
139
|
+
const artifactJSON = JSON.parse(await readFile(artifactPath, 'utf-8')) as NoirCompiledContract;
|
|
140
|
+
const artifactWithoutHash = loadContractArtifact(artifactJSON);
|
|
141
|
+
artifact = {
|
|
142
|
+
...artifactWithoutHash,
|
|
143
|
+
// Artifact hash is *very* expensive to compute, so we do it here once
|
|
144
|
+
// and the TXE contract data provider can cache it
|
|
145
|
+
artifactHash: await computeArtifactHash(artifactWithoutHash),
|
|
146
|
+
};
|
|
114
147
|
this.logger.debug(
|
|
115
148
|
`Deploy ${
|
|
116
149
|
artifact.name
|
|
117
150
|
} with initializer ${initializer}(${decodedArgs}) and public keys hash ${publicKeysHash.toString()}`,
|
|
118
151
|
);
|
|
119
|
-
instance = await
|
|
152
|
+
instance = await getContractInstanceFromInstantiationParams(artifact, {
|
|
120
153
|
constructorArgs: decodedArgs,
|
|
121
154
|
skipArgsDecoding: true,
|
|
122
155
|
salt: Fr.ONE,
|
|
@@ -127,7 +160,7 @@ class TXEDispatcher {
|
|
|
127
160
|
TXEArtifactsCache.set(cacheKey, { artifact, instance });
|
|
128
161
|
}
|
|
129
162
|
|
|
130
|
-
inputs.splice(0,
|
|
163
|
+
inputs.splice(0, 1, artifact, instance, toSingle(secret));
|
|
131
164
|
}
|
|
132
165
|
|
|
133
166
|
async #processAddAccountInputs({ inputs }: TXEForeignCallInput) {
|
|
@@ -135,7 +168,7 @@ class TXEDispatcher {
|
|
|
135
168
|
|
|
136
169
|
const cacheKey = `SchnorrAccountContract-${secret}`;
|
|
137
170
|
|
|
138
|
-
let artifact;
|
|
171
|
+
let artifact: ContractArtifactWithHash;
|
|
139
172
|
let instance;
|
|
140
173
|
|
|
141
174
|
if (TXEArtifactsCache.has(cacheKey)) {
|
|
@@ -144,8 +177,13 @@ class TXEDispatcher {
|
|
|
144
177
|
} else {
|
|
145
178
|
const keys = await deriveKeys(secret);
|
|
146
179
|
const args = [keys.publicKeys.masterIncomingViewingPublicKey.x, keys.publicKeys.masterIncomingViewingPublicKey.y];
|
|
147
|
-
artifact =
|
|
148
|
-
|
|
180
|
+
artifact = {
|
|
181
|
+
...SchnorrAccountContractArtifact,
|
|
182
|
+
// Artifact hash is *very* expensive to compute, so we do it here once
|
|
183
|
+
// and the TXE contract data provider can cache it
|
|
184
|
+
artifactHash: await computeArtifactHash(SchnorrAccountContractArtifact),
|
|
185
|
+
};
|
|
186
|
+
instance = await getContractInstanceFromInstantiationParams(artifact, {
|
|
149
187
|
constructorArgs: args,
|
|
150
188
|
skipArgsDecoding: true,
|
|
151
189
|
salt: Fr.ONE,
|
|
@@ -164,35 +202,28 @@ class TXEDispatcher {
|
|
|
164
202
|
const { session_id: sessionId, function: functionName, inputs } = callData;
|
|
165
203
|
this.logger.debug(`Calling ${functionName} on session ${sessionId}`);
|
|
166
204
|
|
|
167
|
-
if (!
|
|
205
|
+
if (!sessions.has(sessionId)) {
|
|
168
206
|
this.logger.debug(`Creating new session ${sessionId}`);
|
|
169
207
|
if (!this.protocolContracts) {
|
|
170
208
|
this.protocolContracts = await Promise.all(
|
|
171
209
|
protocolContractNames.map(name => new BundledProtocolContractsProvider().getProtocolContractArtifact(name)),
|
|
172
210
|
);
|
|
173
211
|
}
|
|
174
|
-
|
|
212
|
+
sessions.set(sessionId, await TXESession.init(this.protocolContracts));
|
|
175
213
|
}
|
|
176
214
|
|
|
177
215
|
switch (functionName) {
|
|
178
|
-
case '
|
|
179
|
-
TXESessions.delete(sessionId) &&
|
|
180
|
-
this.logger.debug(`Called reset on session ${sessionId}, yeeting it out of existence`);
|
|
181
|
-
return toForeignCallResult([]);
|
|
182
|
-
}
|
|
183
|
-
case 'deploy': {
|
|
216
|
+
case 'txeDeploy': {
|
|
184
217
|
await this.#processDeployInputs(callData);
|
|
185
218
|
break;
|
|
186
219
|
}
|
|
187
|
-
case '
|
|
220
|
+
case 'txeAddAccount': {
|
|
188
221
|
await this.#processAddAccountInputs(callData);
|
|
189
222
|
break;
|
|
190
223
|
}
|
|
191
224
|
}
|
|
192
225
|
|
|
193
|
-
|
|
194
|
-
const response = await (txeService as any)[functionName](...inputs);
|
|
195
|
-
return response;
|
|
226
|
+
return await sessions.get(sessionId)!.processFunction(functionName, inputs);
|
|
196
227
|
}
|
|
197
228
|
}
|
|
198
229
|
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { ContractArtifact } from '@aztec/aztec.js/abi';
|
|
2
|
+
import { CompleteAddress } from '@aztec/aztec.js/addresses';
|
|
3
|
+
import type { ContractInstanceWithAddress } from '@aztec/aztec.js/contracts';
|
|
4
|
+
import { TxHash } from '@aztec/aztec.js/tx';
|
|
5
|
+
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
6
|
+
import type { Fr } from '@aztec/foundation/curves/bn254';
|
|
7
|
+
import type { EventSelector, FunctionSelector } from '@aztec/stdlib/abi';
|
|
8
|
+
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
9
|
+
import type { UInt64 } from '@aztec/stdlib/types';
|
|
10
|
+
|
|
11
|
+
// These interfaces complement the ones defined in PXE, and combined with those contain the full list of oracles used by
|
|
12
|
+
// aztec-nr. In particular, these include the ones needed to run Brillig code associated to #[external("public")] functions that has
|
|
13
|
+
// not been transpiled (e.g. in the context of a Noir test) as well as the ones associated with managing the state of
|
|
14
|
+
// such a Noir test (deploying contracts, manipulating block time, making calls, etc) - the so called 'top level test
|
|
15
|
+
// context'.
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Oracle methods associated with the execution of an Aztec #[external("public")] function.
|
|
19
|
+
*
|
|
20
|
+
* Note that real contracts have their Brillig calls to these be transpiled into opcodes, the oracles are only executed
|
|
21
|
+
* as such when running the original Brillig code, e.g. when invoking functions that interact with a PublicContext
|
|
22
|
+
* directly in a Noir test.
|
|
23
|
+
*/
|
|
24
|
+
export interface IAvmExecutionOracle {
|
|
25
|
+
isAvm: true;
|
|
26
|
+
|
|
27
|
+
avmOpcodeAddress(): Promise<AztecAddress>;
|
|
28
|
+
avmOpcodeSender(): Promise<AztecAddress>;
|
|
29
|
+
avmOpcodeBlockNumber(): Promise<BlockNumber>;
|
|
30
|
+
avmOpcodeTimestamp(): Promise<bigint>;
|
|
31
|
+
avmOpcodeIsStaticCall(): Promise<boolean>;
|
|
32
|
+
avmOpcodeChainId(): Promise<Fr>;
|
|
33
|
+
avmOpcodeVersion(): Promise<Fr>;
|
|
34
|
+
avmOpcodeEmitNullifier(nullifier: Fr): Promise<void>;
|
|
35
|
+
avmOpcodeEmitNoteHash(noteHash: Fr): Promise<void>;
|
|
36
|
+
avmOpcodeNullifierExists(siloedNullifier: Fr): Promise<boolean>;
|
|
37
|
+
avmOpcodeStorageWrite(slot: Fr, value: Fr): Promise<void>;
|
|
38
|
+
avmOpcodeStorageRead(slot: Fr, contractAddress: AztecAddress): Promise<Fr>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Oracle methods associated with the execution of an Aztec Noir test.
|
|
43
|
+
*/
|
|
44
|
+
export interface ITxeExecutionOracle {
|
|
45
|
+
isTxe: true;
|
|
46
|
+
|
|
47
|
+
txeGetDefaultAddress(): AztecAddress;
|
|
48
|
+
txeGetNextBlockNumber(): Promise<BlockNumber>;
|
|
49
|
+
txeGetNextBlockTimestamp(): Promise<UInt64>;
|
|
50
|
+
txeAdvanceBlocksBy(blocks: number): Promise<void>;
|
|
51
|
+
txeAdvanceTimestampBy(duration: UInt64): void;
|
|
52
|
+
txeDeploy(artifact: ContractArtifact, instance: ContractInstanceWithAddress, foreignSecret: Fr): Promise<void>;
|
|
53
|
+
txeCreateAccount(secret: Fr): Promise<CompleteAddress>;
|
|
54
|
+
txeAddAccount(
|
|
55
|
+
artifact: ContractArtifact,
|
|
56
|
+
instance: ContractInstanceWithAddress,
|
|
57
|
+
secret: Fr,
|
|
58
|
+
): Promise<CompleteAddress>;
|
|
59
|
+
txeAddAuthWitness(address: AztecAddress, messageHash: Fr): Promise<void>;
|
|
60
|
+
txeGetLastBlockTimestamp(): Promise<bigint>;
|
|
61
|
+
txeGetLastTxEffects(): Promise<{
|
|
62
|
+
txHash: TxHash;
|
|
63
|
+
noteHashes: Fr[];
|
|
64
|
+
nullifiers: Fr[];
|
|
65
|
+
}>;
|
|
66
|
+
txeGetPrivateEvents(selector: EventSelector, contractAddress: AztecAddress, scope: AztecAddress): Promise<Fr[][]>;
|
|
67
|
+
txePrivateCallNewFlow(
|
|
68
|
+
from: AztecAddress,
|
|
69
|
+
targetContractAddress: AztecAddress,
|
|
70
|
+
functionSelector: FunctionSelector,
|
|
71
|
+
args: Fr[],
|
|
72
|
+
argsHash: Fr,
|
|
73
|
+
isStaticCall: boolean,
|
|
74
|
+
): Promise<Fr[]>;
|
|
75
|
+
txeSimulateUtilityFunction(
|
|
76
|
+
targetContractAddress: AztecAddress,
|
|
77
|
+
functionSelector: FunctionSelector,
|
|
78
|
+
args: Fr[],
|
|
79
|
+
): Promise<Fr[]>;
|
|
80
|
+
txePublicCallNewFlow(
|
|
81
|
+
from: AztecAddress,
|
|
82
|
+
targetContractAddress: AztecAddress,
|
|
83
|
+
calldata: Fr[],
|
|
84
|
+
isStaticCall: boolean,
|
|
85
|
+
): Promise<Fr[]>;
|
|
86
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
2
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
3
|
+
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
4
|
+
import { PublicDataWrite } from '@aztec/stdlib/avm';
|
|
5
|
+
import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
6
|
+
import type { L2Block } from '@aztec/stdlib/block';
|
|
7
|
+
import { computePublicDataTreeLeafSlot, siloNoteHash, siloNullifier } from '@aztec/stdlib/hash';
|
|
8
|
+
import {
|
|
9
|
+
MerkleTreeId,
|
|
10
|
+
type MerkleTreeWriteOperations,
|
|
11
|
+
PublicDataTreeLeaf,
|
|
12
|
+
PublicDataTreeLeafPreimage,
|
|
13
|
+
} from '@aztec/stdlib/trees';
|
|
14
|
+
import { GlobalVariables, TxEffect, TxHash } from '@aztec/stdlib/tx';
|
|
15
|
+
|
|
16
|
+
import { insertTxEffectIntoWorldTrees, makeTXEBlock } from '../utils/block_creation.js';
|
|
17
|
+
import type { IAvmExecutionOracle } from './interfaces.js';
|
|
18
|
+
|
|
19
|
+
export class TXEOraclePublicContext implements IAvmExecutionOracle {
|
|
20
|
+
isAvm = true as const;
|
|
21
|
+
|
|
22
|
+
private logger: Logger;
|
|
23
|
+
private transientUniqueNoteHashes: Fr[] = [];
|
|
24
|
+
private transientSiloedNullifiers: Fr[] = [];
|
|
25
|
+
private publicDataWrites: PublicDataWrite[] = [];
|
|
26
|
+
|
|
27
|
+
constructor(
|
|
28
|
+
private contractAddress: AztecAddress,
|
|
29
|
+
private forkedWorldTrees: MerkleTreeWriteOperations,
|
|
30
|
+
private txRequestHash: Fr,
|
|
31
|
+
private globalVariables: GlobalVariables,
|
|
32
|
+
) {
|
|
33
|
+
this.logger = createLogger('txe:public_context');
|
|
34
|
+
|
|
35
|
+
this.logger.debug('Entering Public Context', {
|
|
36
|
+
contractAddress,
|
|
37
|
+
blockNumber: globalVariables.blockNumber,
|
|
38
|
+
timestamp: globalVariables.timestamp,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
avmOpcodeAddress(): Promise<AztecAddress> {
|
|
43
|
+
return Promise.resolve(this.contractAddress);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
avmOpcodeSender(): Promise<AztecAddress> {
|
|
47
|
+
return Promise.resolve(AztecAddress.ZERO); // todo: change?
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
avmOpcodeBlockNumber(): Promise<BlockNumber> {
|
|
51
|
+
return Promise.resolve(this.globalVariables.blockNumber);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
avmOpcodeTimestamp(): Promise<bigint> {
|
|
55
|
+
return Promise.resolve(this.globalVariables.timestamp);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
avmOpcodeIsStaticCall(): Promise<boolean> {
|
|
59
|
+
return Promise.resolve(false);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
avmOpcodeChainId(): Promise<Fr> {
|
|
63
|
+
return Promise.resolve(this.globalVariables.chainId);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
avmOpcodeVersion(): Promise<Fr> {
|
|
67
|
+
return Promise.resolve(this.globalVariables.version);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async avmOpcodeEmitNullifier(nullifier: Fr) {
|
|
71
|
+
const siloedNullifier = await siloNullifier(this.contractAddress, nullifier);
|
|
72
|
+
this.transientSiloedNullifiers.push(siloedNullifier);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async avmOpcodeEmitNoteHash(noteHash: Fr) {
|
|
76
|
+
const siloedNoteHash = await siloNoteHash(this.contractAddress, noteHash);
|
|
77
|
+
// TODO: make the note hash unique - they are only siloed right now
|
|
78
|
+
this.transientUniqueNoteHashes.push(siloedNoteHash);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async avmOpcodeNullifierExists(siloedNullifier: Fr): Promise<boolean> {
|
|
82
|
+
const treeIndex = (
|
|
83
|
+
await this.forkedWorldTrees.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, [siloedNullifier.toBuffer()])
|
|
84
|
+
)[0];
|
|
85
|
+
const transientIndex = this.transientSiloedNullifiers.find(n => n.equals(siloedNullifier));
|
|
86
|
+
|
|
87
|
+
return treeIndex !== undefined || transientIndex !== undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async avmOpcodeStorageWrite(slot: Fr, value: Fr) {
|
|
91
|
+
this.logger.debug('AVM storage write', { slot, value });
|
|
92
|
+
|
|
93
|
+
const dataWrite = new PublicDataWrite(await computePublicDataTreeLeafSlot(this.contractAddress, slot), value);
|
|
94
|
+
|
|
95
|
+
this.publicDataWrites.push(dataWrite);
|
|
96
|
+
|
|
97
|
+
await this.forkedWorldTrees.sequentialInsert(MerkleTreeId.PUBLIC_DATA_TREE, [
|
|
98
|
+
new PublicDataTreeLeaf(dataWrite.leafSlot, dataWrite.value).toBuffer(),
|
|
99
|
+
]);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async avmOpcodeStorageRead(slot: Fr, contractAddress: AztecAddress): Promise<Fr> {
|
|
103
|
+
const leafSlot = await computePublicDataTreeLeafSlot(contractAddress, slot);
|
|
104
|
+
|
|
105
|
+
const lowLeafResult = await this.forkedWorldTrees.getPreviousValueIndex(
|
|
106
|
+
MerkleTreeId.PUBLIC_DATA_TREE,
|
|
107
|
+
leafSlot.toBigInt(),
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
const value =
|
|
111
|
+
!lowLeafResult || !lowLeafResult.alreadyPresent
|
|
112
|
+
? Fr.ZERO
|
|
113
|
+
: (
|
|
114
|
+
(await this.forkedWorldTrees.getLeafPreimage(
|
|
115
|
+
MerkleTreeId.PUBLIC_DATA_TREE,
|
|
116
|
+
lowLeafResult.index,
|
|
117
|
+
)) as PublicDataTreeLeafPreimage
|
|
118
|
+
).leaf.value;
|
|
119
|
+
|
|
120
|
+
this.logger.debug('AVM storage read', { slot, contractAddress, value });
|
|
121
|
+
|
|
122
|
+
return value;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async close(): Promise<L2Block> {
|
|
126
|
+
this.logger.debug('Exiting Public Context, building block with collected side effects', {
|
|
127
|
+
blockNumber: this.globalVariables.blockNumber,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const txEffect = this.makeTxEffect();
|
|
131
|
+
await insertTxEffectIntoWorldTrees(txEffect, this.forkedWorldTrees);
|
|
132
|
+
|
|
133
|
+
const block = await makeTXEBlock(this.forkedWorldTrees, this.globalVariables, [txEffect]);
|
|
134
|
+
|
|
135
|
+
await this.forkedWorldTrees.close();
|
|
136
|
+
|
|
137
|
+
this.logger.debug('Exited PublicContext with built block', {
|
|
138
|
+
blockNumber: block.number,
|
|
139
|
+
txEffects: block.body.txEffects,
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
return block;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private makeTxEffect(): TxEffect {
|
|
146
|
+
const txEffect = TxEffect.empty();
|
|
147
|
+
|
|
148
|
+
txEffect.noteHashes = this.transientUniqueNoteHashes;
|
|
149
|
+
txEffect.nullifiers = [this.txRequestHash, ...this.transientSiloedNullifiers];
|
|
150
|
+
|
|
151
|
+
txEffect.publicDataWrites = this.publicDataWrites;
|
|
152
|
+
// TODO: support public logs
|
|
153
|
+
|
|
154
|
+
txEffect.txHash = new TxHash(new Fr(this.globalVariables.blockNumber));
|
|
155
|
+
|
|
156
|
+
return txEffect;
|
|
157
|
+
}
|
|
158
|
+
}
|