@aztec/txe 0.0.1-fake-ceab37513c → 0.0.6-commit.a2d1860fe9
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 +1 -1
- 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 +90 -54
- 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 +14 -14
- package/dest/oracle/txe_oracle_public_context.d.ts.map +1 -1
- package/dest/oracle/txe_oracle_public_context.js +36 -40
- package/dest/oracle/txe_oracle_top_level_context.d.ts +65 -0
- package/dest/oracle/txe_oracle_top_level_context.d.ts.map +1 -0
- package/dest/oracle/txe_oracle_top_level_context.js +530 -0
- package/dest/rpc_translator.d.ts +252 -0
- package/dest/rpc_translator.d.ts.map +1 -0
- package/dest/{txe_service/txe_service.js → rpc_translator.js} +258 -156
- package/dest/state_machine/archiver.d.ts +21 -51
- package/dest/state_machine/archiver.d.ts.map +1 -1
- package/dest/state_machine/archiver.js +63 -94
- package/dest/state_machine/dummy_p2p_client.d.ts +20 -13
- package/dest/state_machine/dummy_p2p_client.d.ts.map +1 -1
- package/dest/state_machine/dummy_p2p_client.js +41 -21
- package/dest/state_machine/global_variable_builder.d.ts +6 -17
- package/dest/state_machine/global_variable_builder.d.ts.map +1 -1
- package/dest/state_machine/global_variable_builder.js +16 -23
- package/dest/state_machine/index.d.ts +7 -7
- package/dest/state_machine/index.d.ts.map +1 -1
- package/dest/state_machine/index.js +42 -24
- package/dest/state_machine/mock_epoch_cache.d.ts +14 -10
- package/dest/state_machine/mock_epoch_cache.d.ts.map +1 -1
- package/dest/state_machine/mock_epoch_cache.js +21 -13
- package/dest/state_machine/synchronizer.d.ts +3 -2
- package/dest/state_machine/synchronizer.d.ts.map +1 -1
- package/dest/state_machine/synchronizer.js +5 -4
- package/dest/txe_session.d.ts +44 -48
- package/dest/txe_session.d.ts.map +1 -1
- package/dest/txe_session.js +314 -98
- package/dest/util/encoding.d.ts +616 -17
- package/dest/util/encoding.d.ts.map +1 -1
- package/dest/util/encoding.js +1 -1
- 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_public_contract_data_source.d.ts +9 -8
- package/dest/util/txe_public_contract_data_source.d.ts.map +1 -1
- package/dest/util/txe_public_contract_data_source.js +15 -30
- 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 +58 -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 +18 -17
- package/src/bin/index.ts +1 -1
- package/src/constants.ts +3 -0
- package/src/index.ts +102 -68
- package/src/oracle/interfaces.ts +86 -0
- package/src/oracle/txe_oracle_public_context.ts +40 -80
- package/src/oracle/txe_oracle_top_level_context.ts +756 -0
- package/src/{txe_service/txe_service.ts → rpc_translator.ts} +323 -161
- package/src/state_machine/archiver.ts +63 -117
- package/src/state_machine/dummy_p2p_client.ts +58 -28
- package/src/state_machine/global_variable_builder.ts +25 -42
- package/src/state_machine/index.ts +65 -24
- package/src/state_machine/mock_epoch_cache.ts +25 -20
- package/src/state_machine/synchronizer.ts +6 -5
- package/src/txe_session.ts +441 -121
- package/src/util/encoding.ts +1 -1
- package/src/util/{txe_account_data_provider.ts → txe_account_store.ts} +1 -1
- package/src/util/txe_public_contract_data_source.ts +23 -46
- package/src/utils/block_creation.ts +97 -0
- package/src/utils/tx_effect_creation.ts +30 -0
- package/dest/oracle/txe_oracle.d.ts +0 -124
- package/dest/oracle/txe_oracle.d.ts.map +0 -1
- package/dest/oracle/txe_oracle.js +0 -770
- package/dest/oracle/txe_typed_oracle.d.ts +0 -42
- package/dest/oracle/txe_typed_oracle.d.ts.map +0 -1
- package/dest/oracle/txe_typed_oracle.js +0 -83
- package/dest/txe_constants.d.ts +0 -2
- package/dest/txe_constants.d.ts.map +0 -1
- package/dest/txe_constants.js +0 -7
- package/dest/txe_service/txe_service.d.ts +0 -231
- package/dest/txe_service/txe_service.d.ts.map +0 -1
- 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_contract_data_provider.d.ts +0 -11
- package/dest/util/txe_contract_data_provider.d.ts.map +0 -1
- package/dest/util/txe_contract_data_provider.js +0 -22
- package/src/oracle/txe_oracle.ts +0 -1287
- package/src/oracle/txe_typed_oracle.ts +0 -142
- package/src/txe_constants.ts +0 -9
- package/src/util/txe_contract_data_provider.ts +0 -35
package/dest/txe_session.js
CHANGED
|
@@ -1,38 +1,27 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
2
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
2
3
|
import { createLogger } from '@aztec/foundation/log';
|
|
3
4
|
import { KeyStore } from '@aztec/key-store';
|
|
4
5
|
import { openTmpStore } from '@aztec/kv-store/lmdb-v2';
|
|
5
|
-
import {
|
|
6
|
+
import { AddressStore, AnchorBlockStore, CapsuleStore, JobCoordinator, NoteService, NoteStore, PrivateEventStore, RecipientTaggingStore, SenderAddressBookStore, SenderTaggingStore } from '@aztec/pxe/server';
|
|
7
|
+
import { ExecutionNoteCache, ExecutionTaggingIndexCache, HashedValuesCache, Oracle, PrivateExecutionOracle, UtilityExecutionOracle } from '@aztec/pxe/simulator';
|
|
8
|
+
import { ExecutionError, WASMSimulator, createSimulationError, extractCallStack, resolveAssertionMessageFromError, toACVMWitness } from '@aztec/simulator/client';
|
|
9
|
+
import { FunctionSelector, FunctionType } from '@aztec/stdlib/abi';
|
|
10
|
+
import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
11
|
+
import { GasSettings } from '@aztec/stdlib/gas';
|
|
12
|
+
import { computeProtocolNullifier } from '@aztec/stdlib/hash';
|
|
6
13
|
import { makeGlobalVariables } from '@aztec/stdlib/testing';
|
|
7
|
-
import {
|
|
14
|
+
import { CallContext, TxContext } from '@aztec/stdlib/tx';
|
|
15
|
+
import { z } from 'zod';
|
|
16
|
+
import { DEFAULT_ADDRESS } from './constants.js';
|
|
8
17
|
import { TXEOraclePublicContext } from './oracle/txe_oracle_public_context.js';
|
|
18
|
+
import { TXEOracleTopLevelContext } from './oracle/txe_oracle_top_level_context.js';
|
|
19
|
+
import { RPCTranslator } from './rpc_translator.js';
|
|
20
|
+
import { TXEArchiver } from './state_machine/archiver.js';
|
|
9
21
|
import { TXEStateMachine } from './state_machine/index.js';
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
/**
|
|
14
|
-
* A TXE Session can be ine one of four states, which change as the test progresses and different oracles are called.
|
|
15
|
-
* The current state determines which oracles are available.
|
|
16
|
-
*/ var SessionState = /*#__PURE__*/ function(SessionState) {
|
|
17
|
-
/**
|
|
18
|
-
* The top-level state is the default state, before any other state has been entered. This is where contracts can be
|
|
19
|
-
* deployed, accounts created, blocks mined, etc.
|
|
20
|
-
*/ SessionState[SessionState["TOP_LEVEL"] = 0] = "TOP_LEVEL";
|
|
21
|
-
/**
|
|
22
|
-
* The private state is entered via the `private_context` function. In this state the PXE oracles that `#[private]`
|
|
23
|
-
* functions use are available, such as those related to note retrieval, notification of side-effects, capsule access,
|
|
24
|
-
* etc. */ SessionState[SessionState["PRIVATE"] = 1] = "PRIVATE";
|
|
25
|
-
/**
|
|
26
|
-
* The public state is entered via the `public_context` function. In this state the AVM opcodes that `#[public]`
|
|
27
|
-
* functions execute are resolved as oracles by TXE, since Noir tests are not transpiled. */ SessionState[SessionState["PUBLIC"] = 2] = "PUBLIC";
|
|
28
|
-
/**
|
|
29
|
-
* The utility state is entered via the `utility_context` function. In this state the PXE oracles that `#[utility]`
|
|
30
|
-
* functions use are available, such as those related to (unconstrained) note retrieval, capsule access, public
|
|
31
|
-
* storage reads, etc.
|
|
32
|
-
*/ SessionState[SessionState["UTILITY"] = 3] = "UTILITY";
|
|
33
|
-
return SessionState;
|
|
34
|
-
}(SessionState || {});
|
|
35
|
-
const DEFAULT_ADDRESS = AztecAddress.fromNumber(42);
|
|
22
|
+
import { TXEAccountStore } from './util/txe_account_store.js';
|
|
23
|
+
import { getSingleTxBlockRequestHash, insertTxEffectIntoWorldTrees, makeTXEBlock } from './utils/block_creation.js';
|
|
24
|
+
import { makeTxEffect } from './utils/tx_effect_creation.js';
|
|
36
25
|
/**
|
|
37
26
|
* A `TXESession` corresponds to a Noir `#[test]` function, and handles all of its oracle calls, stores test-specific
|
|
38
27
|
* state, etc., independent of all other tests running in parallel.
|
|
@@ -40,34 +29,77 @@ const DEFAULT_ADDRESS = AztecAddress.fromNumber(42);
|
|
|
40
29
|
logger;
|
|
41
30
|
stateMachine;
|
|
42
31
|
oracleHandler;
|
|
43
|
-
|
|
32
|
+
contractStore;
|
|
33
|
+
noteStore;
|
|
34
|
+
keyStore;
|
|
35
|
+
addressStore;
|
|
36
|
+
accountStore;
|
|
37
|
+
senderTaggingStore;
|
|
38
|
+
recipientTaggingStore;
|
|
39
|
+
senderAddressBookStore;
|
|
40
|
+
capsuleStore;
|
|
41
|
+
privateEventStore;
|
|
42
|
+
jobCoordinator;
|
|
43
|
+
currentJobId;
|
|
44
|
+
chainId;
|
|
45
|
+
version;
|
|
46
|
+
nextBlockTimestamp;
|
|
44
47
|
state;
|
|
45
|
-
|
|
48
|
+
authwits;
|
|
49
|
+
constructor(logger, stateMachine, oracleHandler, contractStore, noteStore, keyStore, addressStore, accountStore, senderTaggingStore, recipientTaggingStore, senderAddressBookStore, capsuleStore, privateEventStore, jobCoordinator, currentJobId, chainId, version, nextBlockTimestamp){
|
|
46
50
|
this.logger = logger;
|
|
47
51
|
this.stateMachine = stateMachine;
|
|
48
52
|
this.oracleHandler = oracleHandler;
|
|
49
|
-
this.
|
|
50
|
-
this.
|
|
53
|
+
this.contractStore = contractStore;
|
|
54
|
+
this.noteStore = noteStore;
|
|
55
|
+
this.keyStore = keyStore;
|
|
56
|
+
this.addressStore = addressStore;
|
|
57
|
+
this.accountStore = accountStore;
|
|
58
|
+
this.senderTaggingStore = senderTaggingStore;
|
|
59
|
+
this.recipientTaggingStore = recipientTaggingStore;
|
|
60
|
+
this.senderAddressBookStore = senderAddressBookStore;
|
|
61
|
+
this.capsuleStore = capsuleStore;
|
|
62
|
+
this.privateEventStore = privateEventStore;
|
|
63
|
+
this.jobCoordinator = jobCoordinator;
|
|
64
|
+
this.currentJobId = currentJobId;
|
|
65
|
+
this.chainId = chainId;
|
|
66
|
+
this.version = version;
|
|
67
|
+
this.nextBlockTimestamp = nextBlockTimestamp;
|
|
68
|
+
this.state = {
|
|
69
|
+
name: 'TOP_LEVEL'
|
|
70
|
+
};
|
|
71
|
+
this.authwits = new Map();
|
|
51
72
|
}
|
|
52
|
-
static async init(
|
|
73
|
+
static async init(contractStore) {
|
|
53
74
|
const store = await openTmpStore('txe-session');
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
const
|
|
57
|
-
const
|
|
58
|
-
const
|
|
59
|
-
const
|
|
75
|
+
const addressStore = new AddressStore(store);
|
|
76
|
+
const privateEventStore = new PrivateEventStore(store);
|
|
77
|
+
const noteStore = new NoteStore(store);
|
|
78
|
+
const senderTaggingStore = new SenderTaggingStore(store);
|
|
79
|
+
const recipientTaggingStore = new RecipientTaggingStore(store);
|
|
80
|
+
const senderAddressBookStore = new SenderAddressBookStore(store);
|
|
81
|
+
const capsuleStore = new CapsuleStore(store);
|
|
60
82
|
const keyStore = new KeyStore(store);
|
|
61
|
-
const
|
|
62
|
-
//
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
83
|
+
const accountStore = new TXEAccountStore(store);
|
|
84
|
+
// Create job coordinator and register staged stores
|
|
85
|
+
const jobCoordinator = new JobCoordinator(store);
|
|
86
|
+
jobCoordinator.registerStores([
|
|
87
|
+
capsuleStore,
|
|
88
|
+
senderTaggingStore,
|
|
89
|
+
recipientTaggingStore,
|
|
90
|
+
privateEventStore,
|
|
91
|
+
noteStore
|
|
92
|
+
]);
|
|
93
|
+
const archiver = new TXEArchiver(store);
|
|
94
|
+
const anchorBlockStore = new AnchorBlockStore(store);
|
|
95
|
+
const stateMachine = await TXEStateMachine.create(archiver, anchorBlockStore, contractStore, noteStore);
|
|
96
|
+
const nextBlockTimestamp = BigInt(Math.floor(new Date().getTime() / 1000));
|
|
97
|
+
const version = new Fr(await stateMachine.node.getVersion());
|
|
98
|
+
const chainId = new Fr(await stateMachine.node.getChainId());
|
|
99
|
+
const initialJobId = jobCoordinator.beginJob();
|
|
100
|
+
const topLevelOracleHandler = new TXEOracleTopLevelContext(stateMachine, contractStore, noteStore, keyStore, addressStore, accountStore, senderTaggingStore, recipientTaggingStore, senderAddressBookStore, capsuleStore, privateEventStore, initialJobId, nextBlockTimestamp, version, chainId, new Map());
|
|
101
|
+
await topLevelOracleHandler.txeAdvanceBlocksBy(1);
|
|
102
|
+
return new TXESession(createLogger('txe:session'), stateMachine, topLevelOracleHandler, contractStore, noteStore, keyStore, addressStore, accountStore, senderTaggingStore, recipientTaggingStore, senderAddressBookStore, capsuleStore, privateEventStore, jobCoordinator, initialJobId, version, chainId, nextBlockTimestamp);
|
|
71
103
|
}
|
|
72
104
|
/**
|
|
73
105
|
* Processes an oracle function invoked by the Noir test associated to this session.
|
|
@@ -75,64 +107,248 @@ const DEFAULT_ADDRESS = AztecAddress.fromNumber(42);
|
|
|
75
107
|
* @param inputs The inputs of the oracle.
|
|
76
108
|
* @returns The oracle return values.
|
|
77
109
|
*/ processFunction(functionName, inputs) {
|
|
78
|
-
|
|
110
|
+
try {
|
|
111
|
+
const translator = new RPCTranslator(this, this.oracleHandler);
|
|
112
|
+
// We perform a runtime validation to check that the function name corresponds to a real oracle handler.
|
|
113
|
+
const validatedFunctionName = z.string().refine((fn)=>typeof translator[fn] === 'function' && !fn.startsWith('handlerAs') && fn !== 'constructor').parse(functionName);
|
|
114
|
+
return translator[validatedFunctionName](...inputs);
|
|
115
|
+
} catch (error) {
|
|
116
|
+
if (error instanceof z.ZodError) {
|
|
117
|
+
throw new Error(`${functionName} does not correspond to any oracle handler available on RPCTranslator`);
|
|
118
|
+
} else if (error instanceof Error) {
|
|
119
|
+
throw new Error(`Execution error while processing function ${functionName} in state ${this.state.name}: ${error.message}`);
|
|
120
|
+
} else {
|
|
121
|
+
throw new Error(`Unknown execution error while processing function ${functionName} in state ${this.state.name}`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
79
124
|
}
|
|
80
|
-
async
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
125
|
+
async enterTopLevelState() {
|
|
126
|
+
switch(this.state.name){
|
|
127
|
+
case 'PRIVATE':
|
|
128
|
+
{
|
|
129
|
+
await this.exitPrivateState();
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
case 'PUBLIC':
|
|
133
|
+
{
|
|
134
|
+
await this.exitPublicState();
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
case 'UTILITY':
|
|
138
|
+
{
|
|
139
|
+
this.exitUtilityContext();
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
case 'TOP_LEVEL':
|
|
143
|
+
{
|
|
144
|
+
throw new Error(`Expected to be in state other than TOP_LEVEL`);
|
|
145
|
+
}
|
|
146
|
+
default:
|
|
147
|
+
{
|
|
148
|
+
this.state;
|
|
149
|
+
}
|
|
97
150
|
}
|
|
98
|
-
|
|
99
|
-
this.
|
|
151
|
+
// Commit all staged stores from the job that was just completed, then begin a new job
|
|
152
|
+
await this.jobCoordinator.commitJob(this.currentJobId);
|
|
153
|
+
this.currentJobId = this.jobCoordinator.beginJob();
|
|
154
|
+
this.oracleHandler = new TXEOracleTopLevelContext(this.stateMachine, this.contractStore, this.noteStore, this.keyStore, this.addressStore, this.accountStore, this.senderTaggingStore, this.recipientTaggingStore, this.senderAddressBookStore, this.capsuleStore, this.privateEventStore, this.currentJobId, this.nextBlockTimestamp, this.version, this.chainId, this.authwits);
|
|
155
|
+
this.state = {
|
|
156
|
+
name: 'TOP_LEVEL'
|
|
157
|
+
};
|
|
158
|
+
this.logger.debug(`Entered state ${this.state.name}`);
|
|
100
159
|
}
|
|
101
|
-
async
|
|
102
|
-
this.
|
|
160
|
+
async enterPrivateState(contractAddress = DEFAULT_ADDRESS, anchorBlockNumber) {
|
|
161
|
+
this.exitTopLevelState();
|
|
162
|
+
// Private execution has two associated block numbers: the anchor block (i.e. the historical block that is used to
|
|
163
|
+
// build the proof), and the *next* block, i.e. the one we'll create once the execution ends, and which will contain
|
|
164
|
+
// a single transaction with the effects of what was done in the test.
|
|
165
|
+
const anchorBlock = await this.stateMachine.node.getBlockHeader(anchorBlockNumber ?? 'latest');
|
|
166
|
+
await new NoteService(this.noteStore, this.stateMachine.node, anchorBlock, this.currentJobId).syncNoteNullifiers(contractAddress, 'ALL_SCOPES');
|
|
167
|
+
const latestBlock = await this.stateMachine.node.getBlockHeader('latest');
|
|
168
|
+
const nextBlockGlobalVariables = makeGlobalVariables(undefined, {
|
|
169
|
+
blockNumber: BlockNumber(latestBlock.globalVariables.blockNumber + 1),
|
|
170
|
+
timestamp: this.nextBlockTimestamp,
|
|
171
|
+
version: this.version,
|
|
172
|
+
chainId: this.chainId
|
|
173
|
+
});
|
|
174
|
+
const txRequestHash = getSingleTxBlockRequestHash(nextBlockGlobalVariables.blockNumber);
|
|
175
|
+
const protocolNullifier = await computeProtocolNullifier(txRequestHash);
|
|
176
|
+
const noteCache = new ExecutionNoteCache(protocolNullifier);
|
|
177
|
+
const taggingIndexCache = new ExecutionTaggingIndexCache();
|
|
178
|
+
const utilityExecutor = this.utilityExecutorForContractSync(anchorBlock);
|
|
179
|
+
this.oracleHandler = new PrivateExecutionOracle({
|
|
180
|
+
argsHash: Fr.ZERO,
|
|
181
|
+
txContext: new TxContext(this.chainId, this.version, GasSettings.empty()),
|
|
182
|
+
callContext: new CallContext(AztecAddress.ZERO, contractAddress, FunctionSelector.empty(), false),
|
|
183
|
+
anchorBlockHeader: anchorBlock,
|
|
184
|
+
utilityExecutor,
|
|
185
|
+
authWitnesses: [],
|
|
186
|
+
capsules: [],
|
|
187
|
+
executionCache: new HashedValuesCache(),
|
|
188
|
+
noteCache,
|
|
189
|
+
taggingIndexCache,
|
|
190
|
+
contractStore: this.contractStore,
|
|
191
|
+
noteStore: this.noteStore,
|
|
192
|
+
keyStore: this.keyStore,
|
|
193
|
+
addressStore: this.addressStore,
|
|
194
|
+
aztecNode: this.stateMachine.node,
|
|
195
|
+
senderTaggingStore: this.senderTaggingStore,
|
|
196
|
+
recipientTaggingStore: this.recipientTaggingStore,
|
|
197
|
+
senderAddressBookStore: this.senderAddressBookStore,
|
|
198
|
+
capsuleStore: this.capsuleStore,
|
|
199
|
+
privateEventStore: this.privateEventStore,
|
|
200
|
+
contractSyncService: this.stateMachine.contractSyncService,
|
|
201
|
+
jobId: this.currentJobId,
|
|
202
|
+
scopes: 'ALL_SCOPES'
|
|
203
|
+
});
|
|
204
|
+
// We store the note and tagging index caches fed into the PrivateExecutionOracle (along with some other auxiliary
|
|
205
|
+
// data) in order to refer to it later, mimicking the way this object is used by the ContractFunctionSimulator. The
|
|
206
|
+
// difference resides in that the simulator has all information needed in order to run the simulation, while ours
|
|
207
|
+
// will be ongoing as the different oracles will be invoked from the Noir test, until eventually the private
|
|
208
|
+
// execution finishes.
|
|
209
|
+
this.state = {
|
|
210
|
+
name: 'PRIVATE',
|
|
211
|
+
nextBlockGlobalVariables,
|
|
212
|
+
noteCache,
|
|
213
|
+
taggingIndexCache
|
|
214
|
+
};
|
|
215
|
+
this.logger.debug(`Entered state ${this.state.name}`);
|
|
216
|
+
return this.oracleHandler.getPrivateContextInputs();
|
|
217
|
+
}
|
|
218
|
+
async enterPublicState(contractAddress) {
|
|
219
|
+
this.exitTopLevelState();
|
|
220
|
+
// The PublicContext will create a block with a single transaction in it, containing the effects of what was done in
|
|
221
|
+
// the test. The block therefore gets the *next* block number and timestamp.
|
|
222
|
+
const latestBlockNumber = (await this.stateMachine.node.getBlockHeader('latest')).globalVariables.blockNumber;
|
|
103
223
|
const globalVariables = makeGlobalVariables(undefined, {
|
|
104
|
-
blockNumber:
|
|
105
|
-
timestamp:
|
|
106
|
-
version:
|
|
107
|
-
chainId:
|
|
224
|
+
blockNumber: BlockNumber(latestBlockNumber + 1),
|
|
225
|
+
timestamp: this.nextBlockTimestamp,
|
|
226
|
+
version: this.version,
|
|
227
|
+
chainId: this.chainId
|
|
228
|
+
});
|
|
229
|
+
this.oracleHandler = new TXEOraclePublicContext(contractAddress ?? DEFAULT_ADDRESS, await this.stateMachine.synchronizer.nativeWorldStateService.fork(), getSingleTxBlockRequestHash(globalVariables.blockNumber), globalVariables);
|
|
230
|
+
this.state = {
|
|
231
|
+
name: 'PUBLIC'
|
|
232
|
+
};
|
|
233
|
+
this.logger.debug(`Entered state ${this.state.name}`);
|
|
234
|
+
}
|
|
235
|
+
async enterUtilityState(contractAddress = DEFAULT_ADDRESS) {
|
|
236
|
+
this.exitTopLevelState();
|
|
237
|
+
const anchorBlockHeader = await this.stateMachine.anchorBlockStore.getBlockHeader();
|
|
238
|
+
// There is no automatic message discovery and contract-driven syncing process in inlined private or utility
|
|
239
|
+
// contexts, which means that known nullifiers are also not searched for, since it is during the tagging sync that
|
|
240
|
+
// we perform this. We therefore search for known nullifiers now, as otherwise notes that were nullified would not
|
|
241
|
+
// be removed from the database.
|
|
242
|
+
// TODO(#12553): make the synchronizer sync here instead and remove this
|
|
243
|
+
await new NoteService(this.noteStore, this.stateMachine.node, anchorBlockHeader, this.currentJobId).syncNoteNullifiers(contractAddress, 'ALL_SCOPES');
|
|
244
|
+
this.oracleHandler = new UtilityExecutionOracle({
|
|
245
|
+
contractAddress,
|
|
246
|
+
authWitnesses: [],
|
|
247
|
+
capsules: [],
|
|
248
|
+
anchorBlockHeader,
|
|
249
|
+
contractStore: this.contractStore,
|
|
250
|
+
noteStore: this.noteStore,
|
|
251
|
+
keyStore: this.keyStore,
|
|
252
|
+
addressStore: this.addressStore,
|
|
253
|
+
aztecNode: this.stateMachine.node,
|
|
254
|
+
recipientTaggingStore: this.recipientTaggingStore,
|
|
255
|
+
senderAddressBookStore: this.senderAddressBookStore,
|
|
256
|
+
capsuleStore: this.capsuleStore,
|
|
257
|
+
privateEventStore: this.privateEventStore,
|
|
258
|
+
jobId: this.currentJobId,
|
|
259
|
+
scopes: 'ALL_SCOPES'
|
|
108
260
|
});
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
this.logger.debug(`Entered state ${
|
|
261
|
+
this.state = {
|
|
262
|
+
name: 'UTILITY'
|
|
263
|
+
};
|
|
264
|
+
this.logger.debug(`Entered state ${this.state.name}`);
|
|
265
|
+
}
|
|
266
|
+
exitTopLevelState() {
|
|
267
|
+
if (this.state.name != 'TOP_LEVEL') {
|
|
268
|
+
throw new Error(`Expected to be in state 'TOP_LEVEL', but got '${this.state.name}' instead`);
|
|
269
|
+
}
|
|
270
|
+
// Note that while all public and private contexts do is build a single block that we then process when exiting
|
|
271
|
+
// those, the top level context performs a large number of actions not captured in the following 'close' call. Among
|
|
272
|
+
// others, it will create empty blocks (via `txeAdvanceBlocksBy` and `deploy`), create blocks with transactions via
|
|
273
|
+
// `txePrivateCallNewFlow` and `txePublicCallNewFlow`, add accounts to PXE via `txeAddAccount`, etc. This is a
|
|
274
|
+
// slight inconsistency in the working model of this class, but is not too bad.
|
|
275
|
+
// TODO: it's quite unfortunate that we need to capture the authwits created to later pass them again when the top
|
|
276
|
+
// level context is re-created. This is because authwits create a temporary utility context that'd otherwise reset
|
|
277
|
+
// the authwits if not persisted, so we'd not be able to pass more than one per execution.
|
|
278
|
+
// Ideally authwits would be passed alongside a contract call instead of pre-seeded.
|
|
279
|
+
[this.nextBlockTimestamp, this.authwits] = this.oracleHandler.close();
|
|
113
280
|
}
|
|
114
|
-
async
|
|
115
|
-
this.
|
|
116
|
-
|
|
117
|
-
this.oracleHandler.txeSetContractAddress(contractAddress);
|
|
281
|
+
async exitPrivateState() {
|
|
282
|
+
if (this.state.name != 'PRIVATE') {
|
|
283
|
+
throw new Error(`Expected to be in state 'PRIVATE', but got '${this.state.name}' instead`);
|
|
118
284
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
285
|
+
this.logger.debug('Exiting Private state, building block with collected side effects', {
|
|
286
|
+
blockNumber: this.state.nextBlockGlobalVariables.blockNumber
|
|
287
|
+
});
|
|
288
|
+
// We rely on the note cache to determine the effects of the transaction. This is incomplete as it doesn't private
|
|
289
|
+
// logs (other effects like enqueued public calls don't need to be considered since those are not allowed).
|
|
290
|
+
const txEffect = await makeTxEffect(this.state.noteCache, this.state.nextBlockGlobalVariables.blockNumber);
|
|
291
|
+
// We build a block holding just this transaction
|
|
292
|
+
const forkedWorldTrees = await this.stateMachine.synchronizer.nativeWorldStateService.fork();
|
|
293
|
+
await insertTxEffectIntoWorldTrees(txEffect, forkedWorldTrees);
|
|
294
|
+
const block = await makeTXEBlock(forkedWorldTrees, this.state.nextBlockGlobalVariables, [
|
|
295
|
+
txEffect
|
|
296
|
+
]);
|
|
297
|
+
await this.stateMachine.handleL2Block(block);
|
|
298
|
+
await forkedWorldTrees.close();
|
|
299
|
+
this.logger.debug('Exited PublicContext with built block', {
|
|
300
|
+
blockNumber: block.number,
|
|
301
|
+
txEffects: block.body.txEffects
|
|
302
|
+
});
|
|
123
303
|
}
|
|
124
|
-
|
|
125
|
-
this.
|
|
126
|
-
|
|
127
|
-
this.oracleHandler.txeSetContractAddress(contractAddress);
|
|
304
|
+
async exitPublicState() {
|
|
305
|
+
if (this.state.name != 'PUBLIC') {
|
|
306
|
+
throw new Error(`Expected to be in state 'PUBLIC', but got '${this.state.name}' instead`);
|
|
128
307
|
}
|
|
129
|
-
|
|
130
|
-
this.
|
|
131
|
-
return Promise.resolve();
|
|
308
|
+
const block = await this.oracleHandler.close();
|
|
309
|
+
await this.stateMachine.handleL2Block(block);
|
|
132
310
|
}
|
|
133
|
-
|
|
134
|
-
if (this.state !=
|
|
135
|
-
throw new Error(`Expected to be in state
|
|
311
|
+
exitUtilityContext() {
|
|
312
|
+
if (this.state.name != 'UTILITY') {
|
|
313
|
+
throw new Error(`Expected to be in state 'UTILITY', but got '${this.state.name}' instead`);
|
|
136
314
|
}
|
|
137
315
|
}
|
|
316
|
+
utilityExecutorForContractSync(anchorBlock) {
|
|
317
|
+
return async (call, scopes)=>{
|
|
318
|
+
const entryPointArtifact = await this.contractStore.getFunctionArtifactWithDebugMetadata(call.to, call.selector);
|
|
319
|
+
if (entryPointArtifact.functionType !== FunctionType.UTILITY) {
|
|
320
|
+
throw new Error(`Cannot run ${entryPointArtifact.functionType} function as utility`);
|
|
321
|
+
}
|
|
322
|
+
try {
|
|
323
|
+
const oracle = new UtilityExecutionOracle({
|
|
324
|
+
contractAddress: call.to,
|
|
325
|
+
authWitnesses: [],
|
|
326
|
+
capsules: [],
|
|
327
|
+
anchorBlockHeader: anchorBlock,
|
|
328
|
+
contractStore: this.contractStore,
|
|
329
|
+
noteStore: this.noteStore,
|
|
330
|
+
keyStore: this.keyStore,
|
|
331
|
+
addressStore: this.addressStore,
|
|
332
|
+
aztecNode: this.stateMachine.node,
|
|
333
|
+
recipientTaggingStore: this.recipientTaggingStore,
|
|
334
|
+
senderAddressBookStore: this.senderAddressBookStore,
|
|
335
|
+
capsuleStore: this.capsuleStore,
|
|
336
|
+
privateEventStore: this.privateEventStore,
|
|
337
|
+
jobId: this.currentJobId,
|
|
338
|
+
scopes
|
|
339
|
+
});
|
|
340
|
+
await new WASMSimulator().executeUserCircuit(toACVMWitness(0, call.args), entryPointArtifact, new Oracle(oracle).toACIRCallback()).catch((err)=>{
|
|
341
|
+
err.message = resolveAssertionMessageFromError(err, entryPointArtifact);
|
|
342
|
+
throw new ExecutionError(err.message, {
|
|
343
|
+
contractAddress: call.to,
|
|
344
|
+
functionSelector: call.selector
|
|
345
|
+
}, extractCallStack(err, entryPointArtifact.debug), {
|
|
346
|
+
cause: err
|
|
347
|
+
});
|
|
348
|
+
});
|
|
349
|
+
} catch (err) {
|
|
350
|
+
throw createSimulationError(err instanceof Error ? err : new Error('Unknown error contract data sync'));
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
}
|
|
138
354
|
}
|