-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathtest_context.ts
194 lines (181 loc) · 6.45 KB
/
test_context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { type BBProverConfig } from '@aztec/bb-prover';
import {
type BlockProver,
type ProcessedTx,
type PublicExecutionRequest,
type ServerCircuitProver,
type Tx,
type TxValidator,
} from '@aztec/circuit-types';
import { type Gas, GlobalVariables, Header, type Nullifier, type TxContext } from '@aztec/circuits.js';
import { type Fr } from '@aztec/foundation/fields';
import { type DebugLogger } from '@aztec/foundation/log';
import { openTmpStore } from '@aztec/kv-store/utils';
import {
type ContractsDataSourcePublicDB,
type PublicExecutionResult,
PublicExecutionResultBuilder,
type PublicExecutor,
PublicProcessor,
RealPublicKernelCircuitSimulator,
type SimulationProvider,
WASMSimulator,
type WorldStatePublicDB,
} from '@aztec/simulator';
import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';
import { type MerkleTreeOperations, MerkleTrees } from '@aztec/world-state';
import * as fs from 'fs/promises';
import { type MockProxy, mock } from 'jest-mock-extended';
import { TestCircuitProver } from '../../../bb-prover/src/test/test_circuit_prover.js';
import { ProvingOrchestrator } from '../orchestrator/orchestrator.js';
import { MemoryProvingQueue } from '../prover-agent/memory-proving-queue.js';
import { ProverAgent } from '../prover-agent/prover-agent.js';
import { getEnvironmentConfig, getSimulationProvider, makeGlobals } from './fixtures.js';
export class TestContext {
constructor(
public publicExecutor: MockProxy<PublicExecutor>,
public publicContractsDB: MockProxy<ContractsDataSourcePublicDB>,
public publicWorldStateDB: MockProxy<WorldStatePublicDB>,
public publicProcessor: PublicProcessor,
public simulationProvider: SimulationProvider,
public globalVariables: GlobalVariables,
public actualDb: MerkleTreeOperations,
public prover: ServerCircuitProver,
public proverAgent: ProverAgent,
public orchestrator: ProvingOrchestrator,
public blockNumber: number,
public directoriesToCleanup: string[],
public logger: DebugLogger,
) {}
public get blockProver() {
return this.orchestrator;
}
static async new(
logger: DebugLogger,
proverCount = 4,
createProver: (bbConfig: BBProverConfig) => Promise<ServerCircuitProver> = _ =>
Promise.resolve(new TestCircuitProver(new NoopTelemetryClient(), new WASMSimulator())),
blockNumber = 3,
) {
const globalVariables = makeGlobals(blockNumber);
const publicExecutor = mock<PublicExecutor>();
const publicContractsDB = mock<ContractsDataSourcePublicDB>();
const publicWorldStateDB = mock<WorldStatePublicDB>();
const publicKernel = new RealPublicKernelCircuitSimulator(new WASMSimulator());
const telemetry = new NoopTelemetryClient();
const actualDb = await MerkleTrees.new(openTmpStore(), telemetry).then(t => t.asLatest());
const processor = new PublicProcessor(
actualDb,
publicExecutor,
publicKernel,
GlobalVariables.empty(),
Header.empty(),
publicContractsDB,
publicWorldStateDB,
telemetry,
);
let localProver: ServerCircuitProver;
const config = await getEnvironmentConfig(logger);
const simulationProvider = await getSimulationProvider({
acvmWorkingDirectory: config?.acvmWorkingDirectory,
acvmBinaryPath: config?.expectedAcvmPath,
});
if (!config) {
localProver = new TestCircuitProver(new NoopTelemetryClient(), simulationProvider);
} else {
const bbConfig: BBProverConfig = {
acvmBinaryPath: config.expectedAcvmPath,
acvmWorkingDirectory: config.acvmWorkingDirectory,
bbBinaryPath: config.expectedBBPath,
bbWorkingDirectory: config.bbWorkingDirectory,
};
localProver = await createProver(bbConfig);
}
const queue = new MemoryProvingQueue(telemetry);
const orchestrator = new ProvingOrchestrator(actualDb, queue, telemetry);
const agent = new ProverAgent(localProver, proverCount);
queue.start();
agent.start(queue);
return new this(
publicExecutor,
publicContractsDB,
publicWorldStateDB,
processor,
simulationProvider,
globalVariables,
actualDb,
localProver,
agent,
orchestrator,
blockNumber,
[config?.directoryToCleanup ?? ''],
logger,
);
}
async cleanup() {
await this.proverAgent.stop();
for (const dir of this.directoriesToCleanup.filter(x => x !== '')) {
await fs.rm(dir, { recursive: true, force: true });
}
}
public async processPublicFunctions(
txs: Tx[],
maxTransactions: number,
blockProver?: BlockProver,
txValidator?: TxValidator<ProcessedTx>,
) {
const defaultExecutorImplementation = (
execution: PublicExecutionRequest,
_globalVariables: GlobalVariables,
availableGas: Gas,
_txContext: TxContext,
_pendingNullifiers: Nullifier[],
transactionFee?: Fr,
_sideEffectCounter?: number,
) => {
for (const tx of txs) {
const allCalls = tx.publicTeardownFunctionCall.isEmpty()
? tx.enqueuedPublicFunctionCalls
: [...tx.enqueuedPublicFunctionCalls, tx.publicTeardownFunctionCall];
for (const request of allCalls) {
if (execution.contractAddress.equals(request.contractAddress)) {
const result = PublicExecutionResultBuilder.fromPublicExecutionRequest({ request }).build({
startGasLeft: availableGas,
endGasLeft: availableGas,
transactionFee,
});
return Promise.resolve(result);
}
}
}
throw new Error(`Unexpected execution request: ${execution}`);
};
return await this.processPublicFunctionsWithMockExecutorImplementation(
txs,
maxTransactions,
blockProver,
txValidator,
defaultExecutorImplementation,
);
}
public async processPublicFunctionsWithMockExecutorImplementation(
txs: Tx[],
maxTransactions: number,
blockProver?: BlockProver,
txValidator?: TxValidator<ProcessedTx>,
executorMock?: (
execution: PublicExecutionRequest,
globalVariables: GlobalVariables,
availableGas: Gas,
txContext: TxContext,
pendingNullifiers: Nullifier[],
transactionFee?: Fr,
sideEffectCounter?: number,
) => Promise<PublicExecutionResult>,
) {
if (executorMock) {
this.publicExecutor.simulate.mockImplementation(executorMock);
}
return await this.publicProcessor.process(txs, maxTransactions, blockProver, txValidator);
}
}