@aztec/simulator 3.0.0-nightly.20251205 → 3.0.0-nightly.20251206
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/public/avm/avm_simulator.d.ts +1 -1
- package/dest/public/avm/avm_simulator.d.ts.map +1 -1
- package/dest/public/avm/avm_simulator.js +5 -4
- package/dest/public/avm/errors.d.ts +7 -1
- package/dest/public/avm/errors.d.ts.map +1 -1
- package/dest/public/avm/errors.js +14 -2
- package/dest/public/avm/opcodes/addressing_mode.js +2 -2
- package/dest/public/avm/opcodes/misc.js +2 -2
- package/dest/public/avm/revert_reason.d.ts +1 -1
- package/dest/public/avm/revert_reason.d.ts.map +1 -1
- package/dest/public/avm/revert_reason.js +3 -2
- package/dest/public/avm/serialization/bytecode_serialization.d.ts +1 -1
- package/dest/public/avm/serialization/bytecode_serialization.d.ts.map +1 -1
- package/dest/public/avm/serialization/bytecode_serialization.js +9 -6
- package/dest/public/debug_fn_name.d.ts +14 -1
- package/dest/public/debug_fn_name.d.ts.map +1 -1
- package/dest/public/debug_fn_name.js +19 -0
- package/dest/public/fixtures/public_tx_simulation_tester.d.ts +8 -2
- package/dest/public/fixtures/public_tx_simulation_tester.d.ts.map +1 -1
- package/dest/public/fixtures/public_tx_simulation_tester.js +12 -4
- package/dest/public/fixtures/simple_contract_data_source.d.ts +2 -2
- package/dest/public/fixtures/simple_contract_data_source.d.ts.map +1 -1
- package/dest/public/fixtures/simple_contract_data_source.js +4 -4
- package/dest/public/fixtures/token_test.d.ts +6 -2
- package/dest/public/fixtures/token_test.d.ts.map +1 -1
- package/dest/public/fixtures/token_test.js +12 -10
- package/dest/public/index.d.ts +2 -2
- package/dest/public/index.d.ts.map +1 -1
- package/dest/public/index.js +1 -1
- package/dest/public/public_processor/public_processor.d.ts +5 -5
- package/dest/public/public_processor/public_processor.d.ts.map +1 -1
- package/dest/public/public_processor/public_processor.js +13 -12
- package/dest/public/public_tx_simulator/cpp_public_tx_simulator.d.ts +6 -34
- package/dest/public/public_tx_simulator/cpp_public_tx_simulator.d.ts.map +1 -1
- package/dest/public/public_tx_simulator/cpp_public_tx_simulator.js +22 -139
- package/dest/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.d.ts +42 -0
- package/dest/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.d.ts.map +1 -0
- package/dest/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.js +86 -0
- package/dest/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.d.ts +30 -0
- package/dest/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.d.ts.map +1 -0
- package/dest/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.js +170 -0
- package/dest/public/public_tx_simulator/index.d.ts +2 -1
- package/dest/public/public_tx_simulator/index.d.ts.map +1 -1
- package/dest/public/public_tx_simulator/index.js +1 -0
- package/dest/public/public_tx_simulator/public_tx_simulator.d.ts +2 -2
- package/dest/public/public_tx_simulator/public_tx_simulator.d.ts.map +1 -1
- package/dest/public/public_tx_simulator/public_tx_simulator.js +7 -3
- package/dest/public/state_manager/state_manager.d.ts +6 -1
- package/dest/public/state_manager/state_manager.d.ts.map +1 -1
- package/dest/public/state_manager/state_manager.js +4 -1
- package/package.json +16 -16
- package/src/public/avm/avm_simulator.ts +7 -4
- package/src/public/avm/errors.ts +16 -2
- package/src/public/avm/opcodes/addressing_mode.ts +2 -2
- package/src/public/avm/opcodes/misc.ts +2 -2
- package/src/public/avm/revert_reason.ts +5 -2
- package/src/public/avm/serialization/bytecode_serialization.ts +17 -6
- package/src/public/debug_fn_name.ts +25 -0
- package/src/public/fixtures/public_tx_simulation_tester.ts +27 -6
- package/src/public/fixtures/simple_contract_data_source.ts +5 -7
- package/src/public/fixtures/token_test.ts +17 -8
- package/src/public/index.ts +5 -1
- package/src/public/public_processor/public_processor.ts +16 -16
- package/src/public/public_tx_simulator/cpp_public_tx_simulator.ts +23 -174
- package/src/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.ts +132 -0
- package/src/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.ts +240 -0
- package/src/public/public_tx_simulator/index.ts +1 -0
- package/src/public/public_tx_simulator/public_tx_simulator.ts +10 -6
- package/src/public/state_manager/state_manager.ts +8 -1
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
} from '../avm/fixtures/utils.js';
|
|
21
21
|
import { PublicContractsDB } from '../public_db_sources.js';
|
|
22
22
|
import { MeasuredCppPublicTxSimulator } from '../public_tx_simulator/cpp_public_tx_simulator.js';
|
|
23
|
-
import {
|
|
23
|
+
import { MeasuredCppVsTsPublicTxSimulator } from '../public_tx_simulator/cpp_vs_ts_public_tx_simulator.js';
|
|
24
24
|
import type { MeasuredPublicTxSimulatorInterface } from '../public_tx_simulator/public_tx_simulator_interface.js';
|
|
25
25
|
import { TestExecutorMetrics } from '../test_executor_metrics.js';
|
|
26
26
|
import { SimpleContractDataSource } from './simple_contract_data_source.js';
|
|
@@ -45,6 +45,17 @@ const defaultConfig: PublicSimulatorConfig = PublicSimulatorConfig.from({
|
|
|
45
45
|
collectStatistics: false,
|
|
46
46
|
});
|
|
47
47
|
|
|
48
|
+
/**
|
|
49
|
+
* Factory type for creating a MeasuredPublicTxSimulatorInterface.
|
|
50
|
+
*/
|
|
51
|
+
export type MeasuredSimulatorFactory = (
|
|
52
|
+
merkleTree: MerkleTreeWriteOperations,
|
|
53
|
+
contractsDB: PublicContractsDB,
|
|
54
|
+
globals: GlobalVariables,
|
|
55
|
+
metrics: TestExecutorMetrics,
|
|
56
|
+
config: PublicSimulatorConfig,
|
|
57
|
+
) => MeasuredPublicTxSimulatorInterface;
|
|
58
|
+
|
|
48
59
|
/**
|
|
49
60
|
* A test class that extends the BaseAvmSimulationTester to enable real-app testing of the PublicTxSimulator.
|
|
50
61
|
* It provides an interface for simulating one transaction at a time and maintains state between subsequent
|
|
@@ -60,15 +71,17 @@ export class PublicTxSimulationTester extends BaseAvmSimulationTester {
|
|
|
60
71
|
contractDataSource: SimpleContractDataSource,
|
|
61
72
|
globals: GlobalVariables = defaultGlobals(),
|
|
62
73
|
private metrics: TestExecutorMetrics = new TestExecutorMetrics(),
|
|
63
|
-
|
|
74
|
+
simulatorFactory?: MeasuredSimulatorFactory,
|
|
64
75
|
config: PublicSimulatorConfig = defaultConfig,
|
|
65
76
|
) {
|
|
66
77
|
super(contractDataSource, merkleTree);
|
|
67
78
|
|
|
68
79
|
const contractsDB = new PublicContractsDB(contractDataSource);
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
80
|
+
if (simulatorFactory) {
|
|
81
|
+
this.simulator = simulatorFactory(merkleTree, contractsDB, globals, this.metrics, config);
|
|
82
|
+
} else {
|
|
83
|
+
this.simulator = new MeasuredCppPublicTxSimulator(merkleTree, contractsDB, globals, this.metrics, config);
|
|
84
|
+
}
|
|
72
85
|
}
|
|
73
86
|
|
|
74
87
|
public static async create(
|
|
@@ -80,7 +93,10 @@ export class PublicTxSimulationTester extends BaseAvmSimulationTester {
|
|
|
80
93
|
): Promise<PublicTxSimulationTester> {
|
|
81
94
|
const contractDataSource = new SimpleContractDataSource();
|
|
82
95
|
const merkleTree = await worldStateService.fork();
|
|
83
|
-
|
|
96
|
+
const simulatorFactory: MeasuredSimulatorFactory = useCppSimulator
|
|
97
|
+
? (mt, cdb, g, m, c) => new MeasuredCppPublicTxSimulator(mt, cdb, g, m, c)
|
|
98
|
+
: (mt, cdb, g, m, c) => new MeasuredCppVsTsPublicTxSimulator(mt, cdb, g, m, c);
|
|
99
|
+
return new PublicTxSimulationTester(merkleTree, contractDataSource, globals, metrics, simulatorFactory, config);
|
|
84
100
|
}
|
|
85
101
|
|
|
86
102
|
public setMetricsPrefix(prefix: string) {
|
|
@@ -137,6 +153,11 @@ export class PublicTxSimulationTester extends BaseAvmSimulationTester {
|
|
|
137
153
|
const txLabelWithCount = `${txLabel}/${this.txCount - 1}`;
|
|
138
154
|
const fullTxLabel = this.metricsPrefix ? `${this.metricsPrefix}/${txLabelWithCount}` : txLabelWithCount;
|
|
139
155
|
|
|
156
|
+
if (!this.simulator) {
|
|
157
|
+
throw new Error(
|
|
158
|
+
'No simulator configured. Pass a simulatorFactory to the constructor or use PublicTxSimulationTester.create()',
|
|
159
|
+
);
|
|
160
|
+
}
|
|
140
161
|
const avmResult = await this.simulator.simulate(tx, fullTxLabel);
|
|
141
162
|
|
|
142
163
|
// Something like this is often useful for debugging:
|
|
@@ -91,19 +91,17 @@ export class SimpleContractDataSource implements ContractDataSource {
|
|
|
91
91
|
return this.contractArtifacts.get(contractInstance!.currentContractClassId.toString());
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
async getDebugFunctionName(address: AztecAddress, selector: FunctionSelector): Promise<string> {
|
|
94
|
+
async getDebugFunctionName(address: AztecAddress, selector: FunctionSelector): Promise<string | undefined> {
|
|
95
95
|
const contractInstance = await this.getContract(address);
|
|
96
96
|
if (!contractInstance) {
|
|
97
|
-
this.logger.warn(
|
|
98
|
-
|
|
99
|
-
);
|
|
100
|
-
return `selector:${selector.toString()}`;
|
|
97
|
+
this.logger.warn(`Couldn't get fn name for debugging. Contract not in tester's ContractDataSource.`);
|
|
98
|
+
return undefined;
|
|
101
99
|
}
|
|
102
100
|
const key = `${contractInstance.currentContractClassId.toString()}:${selector.toString()}`;
|
|
103
101
|
const fnName = this.debugFunctionName.get(key);
|
|
104
102
|
if (!fnName) {
|
|
105
|
-
this.logger.warn(`Couldn't get fn name for debugging
|
|
106
|
-
return
|
|
103
|
+
this.logger.warn(`Couldn't get fn name for debugging...`);
|
|
104
|
+
return undefined;
|
|
107
105
|
}
|
|
108
106
|
return fnName;
|
|
109
107
|
}
|
|
@@ -7,11 +7,17 @@ import type { ContractInstanceWithAddress } from '@aztec/stdlib/contract';
|
|
|
7
7
|
|
|
8
8
|
import { PublicTxSimulationTester } from './public_tx_simulation_tester.js';
|
|
9
9
|
|
|
10
|
+
export type TokenTestOptions = {
|
|
11
|
+
/** Skip return value assertions in balance checks (useful for benchmarking with collectCallMetadata=false) */
|
|
12
|
+
skipReturnValueAssertions?: boolean;
|
|
13
|
+
};
|
|
14
|
+
|
|
10
15
|
export async function tokenTest(
|
|
11
16
|
tester: PublicTxSimulationTester,
|
|
12
17
|
logger: Logger,
|
|
13
18
|
tokenArtifact: ContractArtifact,
|
|
14
19
|
expectToBeTrue: (x: boolean) => void,
|
|
20
|
+
options: TokenTestOptions = {},
|
|
15
21
|
) {
|
|
16
22
|
const timer = new Timer();
|
|
17
23
|
|
|
@@ -36,7 +42,7 @@ export async function tokenTest(
|
|
|
36
42
|
],
|
|
37
43
|
);
|
|
38
44
|
expectToBeTrue(mintResult.revertCode.isOK());
|
|
39
|
-
await checkBalance(tester, token, sender, sender, mintAmount, expectToBeTrue);
|
|
45
|
+
await checkBalance(tester, token, sender, sender, mintAmount, expectToBeTrue, options);
|
|
40
46
|
|
|
41
47
|
const authwitNonce = new Fr(0);
|
|
42
48
|
const transferAmount = 50n;
|
|
@@ -54,8 +60,8 @@ export async function tokenTest(
|
|
|
54
60
|
],
|
|
55
61
|
);
|
|
56
62
|
expectToBeTrue(transferResult.revertCode.isOK());
|
|
57
|
-
await checkBalance(tester, token, sender,
|
|
58
|
-
await checkBalance(tester, token, sender, receiver, transferAmount, expectToBeTrue);
|
|
63
|
+
await checkBalance(tester, token, sender, sender, mintAmount - transferAmount, expectToBeTrue, options);
|
|
64
|
+
await checkBalance(tester, token, sender, receiver, transferAmount, expectToBeTrue, options);
|
|
59
65
|
|
|
60
66
|
// EXECUTE! This means that if using AvmProvingTester subclass, it will PROVE the transaction!
|
|
61
67
|
const burnResult = await tester.executeTxWithLabel(
|
|
@@ -71,7 +77,7 @@ export async function tokenTest(
|
|
|
71
77
|
],
|
|
72
78
|
);
|
|
73
79
|
expectToBeTrue(burnResult.revertCode.isOK());
|
|
74
|
-
await checkBalance(tester, token, sender, receiver, 0n, expectToBeTrue);
|
|
80
|
+
await checkBalance(tester, token, sender, receiver, 0n, expectToBeTrue, options);
|
|
75
81
|
|
|
76
82
|
logger.info(`TokenContract test took ${timer.ms()}ms\n`);
|
|
77
83
|
}
|
|
@@ -116,6 +122,7 @@ async function checkBalance(
|
|
|
116
122
|
account: AztecAddress,
|
|
117
123
|
expectedBalance: bigint,
|
|
118
124
|
expectToBeTrue: (x: boolean) => void,
|
|
125
|
+
options: TokenTestOptions = {},
|
|
119
126
|
) {
|
|
120
127
|
// Strictly simulate this! No need to "execute" (aka prove if using AvmProvingTester subclass).
|
|
121
128
|
const balResult = await tester.simulateTxWithLabel(
|
|
@@ -132,8 +139,10 @@ async function checkBalance(
|
|
|
132
139
|
],
|
|
133
140
|
);
|
|
134
141
|
expectToBeTrue(balResult.revertCode.isOK());
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
142
|
+
if (!options.skipReturnValueAssertions) {
|
|
143
|
+
// should be 1 call with 1 return value that is expectedBalance
|
|
144
|
+
const appLogicReturnValues = balResult.getAppLogicReturnValues();
|
|
145
|
+
expectToBeTrue(appLogicReturnValues.length === 1);
|
|
146
|
+
expectToBeTrue(appLogicReturnValues[0].values?.[0]?.toBigInt() === expectedBalance);
|
|
147
|
+
}
|
|
139
148
|
}
|
package/src/public/index.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
export { PublicContractsDB } from './public_db_sources.js';
|
|
2
2
|
export { GuardedMerkleTreeOperations } from './public_processor/guarded_merkle_tree.js';
|
|
3
3
|
export { PublicProcessor, PublicProcessorFactory } from './public_processor/public_processor.js';
|
|
4
|
-
export {
|
|
4
|
+
export {
|
|
5
|
+
CppPublicTxSimulator,
|
|
6
|
+
type PublicTxSimulatorInterface,
|
|
7
|
+
TelemetryCppPublicTxSimulator,
|
|
8
|
+
} from './public_tx_simulator/index.js';
|
|
5
9
|
export type { PublicTxResult, PublicSimulatorConfig as PublicTxSimulatorConfig } from '@aztec/stdlib/avm';
|
|
6
10
|
export { getCallRequestsWithCalldataByPhase } from './utils.js';
|
|
@@ -51,9 +51,9 @@ import { AssertionError } from 'assert';
|
|
|
51
51
|
|
|
52
52
|
import { PublicContractsDB, PublicTreesDB } from '../public_db_sources.js';
|
|
53
53
|
import {
|
|
54
|
-
type PublicTxSimulator,
|
|
55
54
|
type PublicTxSimulatorConfig,
|
|
56
|
-
|
|
55
|
+
type PublicTxSimulatorInterface,
|
|
56
|
+
TelemetryCppPublicTxSimulator,
|
|
57
57
|
} from '../public_tx_simulator/index.js';
|
|
58
58
|
import { GuardedMerkleTreeOperations } from './guarded_merkle_tree.js';
|
|
59
59
|
import { PublicProcessorMetrics } from './public_processor_metrics.js';
|
|
@@ -99,8 +99,8 @@ export class PublicProcessorFactory {
|
|
|
99
99
|
contractsDB: PublicContractsDB,
|
|
100
100
|
globalVariables: GlobalVariables,
|
|
101
101
|
config?: Partial<PublicTxSimulatorConfig>,
|
|
102
|
-
):
|
|
103
|
-
return new
|
|
102
|
+
): PublicTxSimulatorInterface {
|
|
103
|
+
return new TelemetryCppPublicTxSimulator(merkleTree, contractsDB, globalVariables, this.telemetryClient, config);
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
106
|
|
|
@@ -122,7 +122,7 @@ export class PublicProcessor implements Traceable {
|
|
|
122
122
|
protected globalVariables: GlobalVariables,
|
|
123
123
|
private guardedMerkleTree: GuardedMerkleTreeOperations,
|
|
124
124
|
protected contractsDB: PublicContractsDB,
|
|
125
|
-
protected publicTxSimulator:
|
|
125
|
+
protected publicTxSimulator: PublicTxSimulatorInterface,
|
|
126
126
|
private dateProvider: DateProvider,
|
|
127
127
|
telemetryClient: TelemetryClient = getTelemetryClient(),
|
|
128
128
|
private log = createLogger('simulator:public-processor'),
|
|
@@ -521,13 +521,9 @@ export class PublicProcessor implements Traceable {
|
|
|
521
521
|
private async processTxWithPublicCalls(tx: Tx): Promise<[ProcessedTx, NestedProcessReturnValues[]]> {
|
|
522
522
|
const timer = new Timer();
|
|
523
523
|
|
|
524
|
-
const
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
if (!hints) {
|
|
528
|
-
this.metrics.recordFailedTx();
|
|
529
|
-
throw new Error('Avm proving result was not generated.');
|
|
530
|
-
}
|
|
524
|
+
const result = await this.publicTxSimulator.simulate(tx);
|
|
525
|
+
// TODO: use the callStackMetadata here to extract more data about public execution
|
|
526
|
+
const { hints, publicInputs, gasUsed, revertCode /*callStackMetadata*/ } = result;
|
|
531
527
|
|
|
532
528
|
const contractClassLogs = revertCode.isOK()
|
|
533
529
|
? tx.getContractClassLogs()
|
|
@@ -539,9 +535,13 @@ export class PublicProcessor implements Traceable {
|
|
|
539
535
|
);
|
|
540
536
|
|
|
541
537
|
// TODO(fcarreiro): remove phase count metric.
|
|
542
|
-
const phaseCount = 1;
|
|
543
538
|
const durationMs = timer.ms();
|
|
544
|
-
this.metrics.recordTx(phaseCount, durationMs, gasUsed.publicGas);
|
|
539
|
+
this.metrics.recordTx(/*phaseCount=*/ 1, durationMs, gasUsed.publicGas);
|
|
540
|
+
|
|
541
|
+
// Extract the return values from the call stack metadata.
|
|
542
|
+
const appLogicReturnValues: NestedProcessReturnValues[] = result.getAppLogicReturnValues();
|
|
543
|
+
// Extract the revert reason from the call stack metadata.
|
|
544
|
+
const revertReason = result.findRevertReason();
|
|
545
545
|
|
|
546
546
|
const processedTx = makeProcessedTxFromTxWithPublicCalls(
|
|
547
547
|
tx,
|
|
@@ -551,7 +551,7 @@ export class PublicProcessor implements Traceable {
|
|
|
551
551
|
revertReason,
|
|
552
552
|
);
|
|
553
553
|
|
|
554
|
-
return [processedTx, appLogicReturnValues
|
|
554
|
+
return [processedTx, appLogicReturnValues];
|
|
555
555
|
}
|
|
556
556
|
|
|
557
557
|
/**
|
|
@@ -559,7 +559,7 @@ export class PublicProcessor implements Traceable {
|
|
|
559
559
|
*/
|
|
560
560
|
private static generateProvingRequest(
|
|
561
561
|
publicInputs: AvmCircuitPublicInputs,
|
|
562
|
-
hints: AvmExecutionHints,
|
|
562
|
+
hints: AvmExecutionHints = AvmExecutionHints.empty(),
|
|
563
563
|
): AvmProvingRequest {
|
|
564
564
|
return {
|
|
565
565
|
type: ProvingRequestType.PUBLIC_VM,
|
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
2
|
-
import {
|
|
3
|
-
import { avmSimulate, avmSimulateWithHintedDbs } from '@aztec/native';
|
|
1
|
+
import { type Logger, createLogger, logLevel } from '@aztec/foundation/log';
|
|
2
|
+
import { avmSimulate } from '@aztec/native';
|
|
4
3
|
import { ProtocolContractsList } from '@aztec/protocol-contracts';
|
|
5
4
|
import {
|
|
6
|
-
AvmCircuitInputs,
|
|
7
5
|
AvmFastSimulationInputs,
|
|
8
6
|
AvmTxHint,
|
|
9
7
|
type PublicSimulatorConfig,
|
|
@@ -12,11 +10,13 @@ import {
|
|
|
12
10
|
} from '@aztec/stdlib/avm';
|
|
13
11
|
import { SimulationError } from '@aztec/stdlib/errors';
|
|
14
12
|
import type { MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
|
|
15
|
-
import type { GlobalVariables,
|
|
13
|
+
import type { GlobalVariables, Tx } from '@aztec/stdlib/tx';
|
|
16
14
|
import { WorldStateRevisionWithHandle } from '@aztec/stdlib/world-state';
|
|
15
|
+
import { type TelemetryClient, type Tracer, getTelemetryClient } from '@aztec/telemetry-client';
|
|
17
16
|
|
|
18
17
|
import { strict as assert } from 'assert';
|
|
19
18
|
|
|
19
|
+
import { ExecutorMetrics } from '../executor_metrics.js';
|
|
20
20
|
import type { ExecutorMetricsInterface } from '../executor_metrics_interface.js';
|
|
21
21
|
import type { PublicContractsDB } from '../public_db_sources.js';
|
|
22
22
|
import { ContractProviderForCpp } from './contract_provider_for_cpp.js';
|
|
@@ -30,9 +30,6 @@ import type {
|
|
|
30
30
|
* C++ implementation of PublicTxSimulator using the C++ simulator.
|
|
31
31
|
* The C++ simulator accesses the world state directly/natively within C++.
|
|
32
32
|
* For contract DB accesses, it makes callbacks through NAPI back to the TS PublicContractsDB cache.
|
|
33
|
-
*
|
|
34
|
-
* TODO(dbanks12): for now this still simulates first in TS to generate hints and public inputs,
|
|
35
|
-
* since the C++ simulator doesn't have hinting & PI generation logic yet.
|
|
36
33
|
*/
|
|
37
34
|
export class CppPublicTxSimulator extends PublicTxSimulator implements PublicTxSimulatorInterface {
|
|
38
35
|
protected override log: Logger;
|
|
@@ -59,33 +56,6 @@ export class CppPublicTxSimulator extends PublicTxSimulator implements PublicTxS
|
|
|
59
56
|
txHash,
|
|
60
57
|
});
|
|
61
58
|
|
|
62
|
-
// TODO(dbanks12): remove the first run with TS that hints!
|
|
63
|
-
// Run TS simulation to generate hints and public inputs
|
|
64
|
-
this.log.debug(`Running TS simulation for tx ${txHash}`);
|
|
65
|
-
|
|
66
|
-
// create checkpoint for ws
|
|
67
|
-
let tsResult: PublicTxResult | undefined;
|
|
68
|
-
let tsStateRef: StateReference | undefined;
|
|
69
|
-
await this.merkleTree.createCheckpoint();
|
|
70
|
-
this.contractsDB.createCheckpoint();
|
|
71
|
-
try {
|
|
72
|
-
// Run the full TypeScript simulation using the parent class
|
|
73
|
-
// This will modify the merkle tree with the transaction's state changes
|
|
74
|
-
tsResult = await super.simulate(tx);
|
|
75
|
-
this.log.debug(`TS simulation completed for tx ${txHash}`);
|
|
76
|
-
|
|
77
|
-
tsStateRef = await this.merkleTree.getStateReference(); // capture tree roots for later comparsion
|
|
78
|
-
} catch (error: any) {
|
|
79
|
-
this.log.warn(`TS simulation failed, but still continuing with C++ simulation: ${error.message} ${error.stack}`);
|
|
80
|
-
} finally {
|
|
81
|
-
// revert checkpoint for ws and clear contract db changes
|
|
82
|
-
// (cpp should reapply exactly the same changes if there are no bugs)
|
|
83
|
-
await this.merkleTree.revertCheckpoint();
|
|
84
|
-
this.contractsDB.revertCheckpoint();
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
this.log.debug(`Running C++ simulation for tx ${txHash}`);
|
|
88
|
-
|
|
89
59
|
// Using the "as WorldStateRevisionWithHandle" is a bit of a "trust me bro", hence the assert.
|
|
90
60
|
let wsRevision = this.merkleTree.getRevision();
|
|
91
61
|
assert(
|
|
@@ -95,7 +65,7 @@ export class CppPublicTxSimulator extends PublicTxSimulator implements PublicTxS
|
|
|
95
65
|
const wsCppHandle = (wsRevision as WorldStateRevisionWithHandle).handle;
|
|
96
66
|
wsRevision = wsRevision.toWorldStateRevision(); // for msgpack serialization, we don't include the handle in the type
|
|
97
67
|
|
|
98
|
-
this.log.
|
|
68
|
+
this.log.trace(`Running C++ simulation with world state revision ${JSON.stringify(wsRevision)}`);
|
|
99
69
|
|
|
100
70
|
// Create the fast simulation inputs
|
|
101
71
|
const txHint = AvmTxHint.fromTx(tx, this.globalVariables.gasFees);
|
|
@@ -112,62 +82,32 @@ export class CppPublicTxSimulator extends PublicTxSimulator implements PublicTxS
|
|
|
112
82
|
const contractProvider = new ContractProviderForCpp(this.contractsDB, this.globalVariables);
|
|
113
83
|
|
|
114
84
|
// Serialize to msgpack and call the C++ simulator
|
|
115
|
-
this.log.
|
|
85
|
+
this.log.trace(`Serializing fast simulation inputs to msgpack...`);
|
|
116
86
|
const inputBuffer = fastSimInputs.serializeWithMessagePack();
|
|
117
87
|
|
|
118
88
|
let resultBuffer: Buffer;
|
|
119
89
|
try {
|
|
120
|
-
this.log.
|
|
121
|
-
resultBuffer = await avmSimulate(inputBuffer, contractProvider, wsCppHandle);
|
|
90
|
+
this.log.debug(`Calling C++ simulator for tx ${txHash}`);
|
|
91
|
+
resultBuffer = await avmSimulate(inputBuffer, contractProvider, wsCppHandle, logLevel);
|
|
122
92
|
} catch (error: any) {
|
|
123
93
|
throw new SimulationError(`C++ simulation failed: ${error.message}`, []);
|
|
124
94
|
}
|
|
125
95
|
|
|
126
96
|
// If we've reached this point, C++ succeeded during simulation,
|
|
127
|
-
// so we assert that TS also succeeded.
|
|
128
|
-
assert(tsResult !== undefined, 'TS simulation should have succeeded if C++ succeeded');
|
|
129
|
-
assert(tsStateRef !== undefined, 'TS state reference should have been captured if C++ succeeded');
|
|
130
97
|
|
|
131
98
|
// Deserialize the msgpack result
|
|
132
|
-
this.log.
|
|
99
|
+
this.log.trace(`Deserializing C++ from buffer (size: ${resultBuffer.length})...`);
|
|
133
100
|
const cppResultJSON: object = deserializeFromMessagePack(resultBuffer);
|
|
134
|
-
|
|
135
|
-
writeTestData(
|
|
136
|
-
`barretenberg/cpp/src/barretenberg/vm2/testing/tx_result_${txHash}.testdata.bin`,
|
|
137
|
-
resultBuffer,
|
|
138
|
-
/*raw=*/ true,
|
|
139
|
-
);
|
|
140
|
-
this.log.verbose(`Deserializing C++ result to PublicTxResult...`);
|
|
101
|
+
this.log.trace(`Deserializing C++ result to PublicTxResult...`);
|
|
141
102
|
const cppResult = PublicTxResult.fromPlainObject(cppResultJSON);
|
|
142
|
-
this.log.verbose(`Done.`);
|
|
143
|
-
// TODO(fcarreiro): complete this.
|
|
144
|
-
assert(cppResult.revertCode.equals(tsResult.revertCode));
|
|
145
|
-
assert(cppResult.gasUsed.totalGas.equals(tsResult.gasUsed.totalGas));
|
|
146
|
-
assert(cppResult.gasUsed.publicGas.equals(tsResult.gasUsed.publicGas));
|
|
147
|
-
assert(cppResult.gasUsed.teardownGas.equals(tsResult.gasUsed.teardownGas));
|
|
148
|
-
assert(cppResult.gasUsed.billedGas.equals(tsResult.gasUsed.billedGas));
|
|
149
|
-
assert(cppResult.publicInputs.toBuffer().equals(tsResult.publicInputs.toBuffer()));
|
|
150
|
-
if (this.config?.collectCallMetadata) {
|
|
151
|
-
assert(cppResult.appLogicReturnValues.length == tsResult.appLogicReturnValues.length);
|
|
152
|
-
assert(cppResult.appLogicReturnValues.every((v, i) => v.equals(tsResult.appLogicReturnValues[i])));
|
|
153
|
-
}
|
|
154
103
|
|
|
155
|
-
|
|
156
|
-
const cppStateRef = await this.merkleTree.getStateReference();
|
|
157
|
-
assert(
|
|
158
|
-
cppStateRef.equals(tsStateRef),
|
|
159
|
-
`Tree roots mismatch between TS and C++ public simulations for tx ${txHash}`,
|
|
160
|
-
);
|
|
161
|
-
|
|
162
|
-
this.log.debug(`C++ simulation completed for tx ${txHash}`, {
|
|
104
|
+
this.log.trace(`C++ simulation completed for tx ${txHash}`, {
|
|
163
105
|
txHash,
|
|
164
|
-
reverted: !
|
|
165
|
-
|
|
166
|
-
cppGasUsed: tsResult.gasUsed.totalGas.l2Gas,
|
|
106
|
+
reverted: !cppResult.revertCode.isOK(),
|
|
107
|
+
cppGasUsed: cppResult.gasUsed.totalGas.l2Gas,
|
|
167
108
|
});
|
|
168
109
|
|
|
169
|
-
|
|
170
|
-
return tsResult;
|
|
110
|
+
return cppResult;
|
|
171
111
|
}
|
|
172
112
|
}
|
|
173
113
|
|
|
@@ -195,112 +135,21 @@ export class MeasuredCppPublicTxSimulator extends CppPublicTxSimulator implement
|
|
|
195
135
|
}
|
|
196
136
|
|
|
197
137
|
/**
|
|
198
|
-
* C++
|
|
199
|
-
* This implementation runs TS simulation first to collect all hints,
|
|
200
|
-
* then passes the complete AvmCircuitInputs (hints + public inputs)
|
|
201
|
-
* to C++ to run hinted simulation.
|
|
138
|
+
* A C++ public tx simulator that tracks runtime/production metrics with telemetry.
|
|
202
139
|
*/
|
|
203
|
-
export class
|
|
204
|
-
|
|
140
|
+
export class TelemetryCppPublicTxSimulator extends MeasuredCppPublicTxSimulator {
|
|
141
|
+
/* tracer needed by trackSpans */
|
|
142
|
+
public readonly tracer: Tracer;
|
|
205
143
|
|
|
206
144
|
constructor(
|
|
207
145
|
merkleTree: MerkleTreeWriteOperations,
|
|
208
146
|
contractsDB: PublicContractsDB,
|
|
209
147
|
globalVariables: GlobalVariables,
|
|
148
|
+
telemetryClient: TelemetryClient = getTelemetryClient(),
|
|
210
149
|
config?: Partial<PublicSimulatorConfig>,
|
|
211
150
|
) {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Simulate a transaction's public portion using the C++ vm2 simulator with hinted DBs.
|
|
218
|
-
*
|
|
219
|
-
* This implementation:
|
|
220
|
-
* 1. Runs the full TypeScript simulation to generate AvmCircuitInputs (hints + public inputs)
|
|
221
|
-
* 2. Passes the complete AvmCircuitInputs to C++ to run hinted simulation
|
|
222
|
-
*
|
|
223
|
-
* @param tx - The transaction to simulate.
|
|
224
|
-
* @returns The result of the transaction's public execution.
|
|
225
|
-
*/
|
|
226
|
-
public override async simulate(tx: Tx): Promise<PublicTxResult> {
|
|
227
|
-
const txHash = this.computeTxHash(tx);
|
|
228
|
-
this.log.debug(`C++ hinted DB simulation of ${tx.publicFunctionCalldata.length} public calls for tx ${txHash}`, {
|
|
229
|
-
txHash,
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
// First, run TS simulation to generate hints and public inputs
|
|
233
|
-
this.log.debug(`Running TS simulation for tx ${txHash}`);
|
|
234
|
-
|
|
235
|
-
// Run the full TypeScript simulation using the parent class
|
|
236
|
-
// This will modify the merkle tree with the transaction's state changes
|
|
237
|
-
const tsResult = await super.simulate(tx);
|
|
238
|
-
this.log.debug(`TS simulation succeeded for tx ${txHash}`);
|
|
239
|
-
|
|
240
|
-
// Extract the full AvmCircuitInputs from the TS result
|
|
241
|
-
const avmCircuitInputs = new AvmCircuitInputs(tsResult.hints!, tsResult.publicInputs);
|
|
242
|
-
|
|
243
|
-
// Second, run C++ simulation with hinted DBs
|
|
244
|
-
this.log.debug(`Running C++ simulation with hinted DBs for tx ${txHash}`);
|
|
245
|
-
|
|
246
|
-
// Serialize to msgpack and call the C++ simulator
|
|
247
|
-
const inputBuffer = avmCircuitInputs.serializeWithMessagePack();
|
|
248
|
-
|
|
249
|
-
let resultBuffer: Buffer;
|
|
250
|
-
try {
|
|
251
|
-
resultBuffer = await avmSimulateWithHintedDbs(inputBuffer);
|
|
252
|
-
} catch (error: any) {
|
|
253
|
-
throw new SimulationError(`C++ hinted simulation failed: ${error.message}`, []);
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
// Deserialize the msgpack result
|
|
257
|
-
const cppResultJSON: object = deserializeFromMessagePack(resultBuffer);
|
|
258
|
-
const cppResult = PublicTxResult.fromPlainObject(cppResultJSON);
|
|
259
|
-
|
|
260
|
-
assert(cppResult.revertCode.equals(tsResult.revertCode));
|
|
261
|
-
assert(cppResult.gasUsed.totalGas.equals(tsResult.gasUsed.totalGas));
|
|
262
|
-
|
|
263
|
-
this.log.debug(`C++ hinted simulation completed for tx ${txHash}`, {
|
|
264
|
-
txHash,
|
|
265
|
-
reverted: !tsResult.revertCode.isOK(),
|
|
266
|
-
tsGasUsed: tsResult.gasUsed.totalGas.l2Gas,
|
|
267
|
-
cppGasUsed: tsResult.gasUsed.totalGas.l2Gas,
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
// TODO(fcarreiro): complete this.
|
|
271
|
-
return tsResult;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Class to record metrics for simulation.
|
|
277
|
-
*
|
|
278
|
-
* Note(dbanks12): We might not be able to collect all the same metrics in C++ as we do in TS!
|
|
279
|
-
* Unless we move some of the metrics collection to C++, we don't have inner functions exposed
|
|
280
|
-
* to TS for tracking.
|
|
281
|
-
*/
|
|
282
|
-
export class MeasuredCppPublicTxSimulatorHintedDbs
|
|
283
|
-
extends CppPublicTxSimulatorHintedDbs
|
|
284
|
-
implements MeasuredPublicTxSimulatorInterface
|
|
285
|
-
{
|
|
286
|
-
constructor(
|
|
287
|
-
merkleTree: MerkleTreeWriteOperations,
|
|
288
|
-
contractsDB: PublicContractsDB,
|
|
289
|
-
globalVariables: GlobalVariables,
|
|
290
|
-
protected readonly metrics: ExecutorMetricsInterface,
|
|
291
|
-
config?: Partial<PublicSimulatorConfig>,
|
|
292
|
-
) {
|
|
293
|
-
super(merkleTree, contractsDB, globalVariables, config);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
public override async simulate(tx: Tx, txLabel: string = 'unlabeledTx'): Promise<PublicTxResult> {
|
|
297
|
-
this.metrics.startRecordingTxSimulation(txLabel);
|
|
298
|
-
let result: PublicTxResult | undefined;
|
|
299
|
-
try {
|
|
300
|
-
result = await super.simulate(tx);
|
|
301
|
-
} finally {
|
|
302
|
-
this.metrics.stopRecordingTxSimulation(txLabel, result?.gasUsed, result?.revertCode);
|
|
303
|
-
}
|
|
304
|
-
return result;
|
|
151
|
+
const metrics = new ExecutorMetrics(telemetryClient, 'CppPublicTxSimulator');
|
|
152
|
+
super(merkleTree, contractsDB, globalVariables, metrics, config);
|
|
153
|
+
this.tracer = metrics.tracer;
|
|
305
154
|
}
|
|
306
155
|
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { type Logger, createLogger, logLevel } from '@aztec/foundation/log';
|
|
2
|
+
import { avmSimulateWithHintedDbs } from '@aztec/native';
|
|
3
|
+
import {
|
|
4
|
+
AvmCircuitInputs,
|
|
5
|
+
type PublicSimulatorConfig,
|
|
6
|
+
PublicTxResult,
|
|
7
|
+
deserializeFromMessagePack,
|
|
8
|
+
} from '@aztec/stdlib/avm';
|
|
9
|
+
import { SimulationError } from '@aztec/stdlib/errors';
|
|
10
|
+
import type { MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
|
|
11
|
+
import type { GlobalVariables, Tx } from '@aztec/stdlib/tx';
|
|
12
|
+
|
|
13
|
+
import { strict as assert } from 'assert';
|
|
14
|
+
|
|
15
|
+
import type { ExecutorMetricsInterface } from '../executor_metrics_interface.js';
|
|
16
|
+
import type { PublicContractsDB } from '../public_db_sources.js';
|
|
17
|
+
import { PublicTxSimulator } from './public_tx_simulator.js';
|
|
18
|
+
import type {
|
|
19
|
+
MeasuredPublicTxSimulatorInterface,
|
|
20
|
+
PublicTxSimulatorInterface,
|
|
21
|
+
} from './public_tx_simulator_interface.js';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* C++ implementation of PublicTxSimulator using pre-collected hints.
|
|
25
|
+
* This implementation runs TS simulation first to collect all hints,
|
|
26
|
+
* then passes the complete AvmCircuitInputs (hints + public inputs)
|
|
27
|
+
* to C++ to run hinted simulation.
|
|
28
|
+
*/
|
|
29
|
+
export class CppPublicTxSimulatorHintedDbs extends PublicTxSimulator implements PublicTxSimulatorInterface {
|
|
30
|
+
protected override log: Logger;
|
|
31
|
+
|
|
32
|
+
constructor(
|
|
33
|
+
merkleTree: MerkleTreeWriteOperations,
|
|
34
|
+
contractsDB: PublicContractsDB,
|
|
35
|
+
globalVariables: GlobalVariables,
|
|
36
|
+
config?: Partial<PublicSimulatorConfig>,
|
|
37
|
+
) {
|
|
38
|
+
super(merkleTree, contractsDB, globalVariables, config);
|
|
39
|
+
this.log = createLogger(`simulator:cpp_public_tx_simulator_hinted_dbs`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Simulate a transaction's public portion using the C++ vm2 simulator with hinted DBs.
|
|
44
|
+
*
|
|
45
|
+
* This implementation:
|
|
46
|
+
* 1. Runs the full TypeScript simulation to generate AvmCircuitInputs (hints + public inputs)
|
|
47
|
+
* 2. Passes the complete AvmCircuitInputs to C++ to run hinted simulation
|
|
48
|
+
*
|
|
49
|
+
* @param tx - The transaction to simulate.
|
|
50
|
+
* @returns The result of the transaction's public execution.
|
|
51
|
+
*/
|
|
52
|
+
public override async simulate(tx: Tx): Promise<PublicTxResult> {
|
|
53
|
+
const txHash = this.computeTxHash(tx);
|
|
54
|
+
this.log.debug(`C++ hinted DB simulation of ${tx.publicFunctionCalldata.length} public calls for tx ${txHash}`, {
|
|
55
|
+
txHash,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// First, run TS simulation to generate hints and public inputs
|
|
59
|
+
this.log.debug(`Running TS simulation for tx ${txHash}`);
|
|
60
|
+
|
|
61
|
+
// Run the full TypeScript simulation using the parent class
|
|
62
|
+
// This will modify the merkle tree with the transaction's state changes
|
|
63
|
+
const tsResult = await super.simulate(tx);
|
|
64
|
+
this.log.debug(`TS simulation succeeded for tx ${txHash}`);
|
|
65
|
+
|
|
66
|
+
// Extract the full AvmCircuitInputs from the TS result
|
|
67
|
+
const avmCircuitInputs = new AvmCircuitInputs(tsResult.hints!, tsResult.publicInputs);
|
|
68
|
+
|
|
69
|
+
// Second, run C++ simulation with hinted DBs
|
|
70
|
+
this.log.debug(`Running C++ simulation with hinted DBs for tx ${txHash}`);
|
|
71
|
+
|
|
72
|
+
// Serialize to msgpack and call the C++ simulator
|
|
73
|
+
const inputBuffer = avmCircuitInputs.serializeWithMessagePack();
|
|
74
|
+
|
|
75
|
+
let resultBuffer: Buffer;
|
|
76
|
+
try {
|
|
77
|
+
resultBuffer = await avmSimulateWithHintedDbs(inputBuffer, logLevel);
|
|
78
|
+
} catch (error: any) {
|
|
79
|
+
throw new SimulationError(`C++ hinted simulation failed: ${error.message}`, []);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Deserialize the msgpack result
|
|
83
|
+
const cppResultJSON: object = deserializeFromMessagePack(resultBuffer);
|
|
84
|
+
const cppResult = PublicTxResult.fromPlainObject(cppResultJSON);
|
|
85
|
+
|
|
86
|
+
assert(cppResult.revertCode.equals(tsResult.revertCode));
|
|
87
|
+
assert(cppResult.gasUsed.totalGas.equals(tsResult.gasUsed.totalGas));
|
|
88
|
+
|
|
89
|
+
this.log.debug(`C++ hinted simulation completed for tx ${txHash}`, {
|
|
90
|
+
txHash,
|
|
91
|
+
reverted: !tsResult.revertCode.isOK(),
|
|
92
|
+
tsGasUsed: tsResult.gasUsed.totalGas.l2Gas,
|
|
93
|
+
cppGasUsed: tsResult.gasUsed.totalGas.l2Gas,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// TODO(fcarreiro): complete this.
|
|
97
|
+
return tsResult;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Class to record metrics for simulation.
|
|
103
|
+
*
|
|
104
|
+
* Note(dbanks12): We might not be able to collect all the same metrics in C++ as we do in TS!
|
|
105
|
+
* Unless we move some of the metrics collection to C++, we don't have inner functions exposed
|
|
106
|
+
* to TS for tracking.
|
|
107
|
+
*/
|
|
108
|
+
export class MeasuredCppPublicTxSimulatorHintedDbs
|
|
109
|
+
extends CppPublicTxSimulatorHintedDbs
|
|
110
|
+
implements MeasuredPublicTxSimulatorInterface
|
|
111
|
+
{
|
|
112
|
+
constructor(
|
|
113
|
+
merkleTree: MerkleTreeWriteOperations,
|
|
114
|
+
contractsDB: PublicContractsDB,
|
|
115
|
+
globalVariables: GlobalVariables,
|
|
116
|
+
protected readonly metrics: ExecutorMetricsInterface,
|
|
117
|
+
config?: Partial<PublicSimulatorConfig>,
|
|
118
|
+
) {
|
|
119
|
+
super(merkleTree, contractsDB, globalVariables, config);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
public override async simulate(tx: Tx, txLabel: string = 'unlabeledTx'): Promise<PublicTxResult> {
|
|
123
|
+
this.metrics.startRecordingTxSimulation(txLabel);
|
|
124
|
+
let result: PublicTxResult | undefined;
|
|
125
|
+
try {
|
|
126
|
+
result = await super.simulate(tx);
|
|
127
|
+
} finally {
|
|
128
|
+
this.metrics.stopRecordingTxSimulation(txLabel, result?.gasUsed, result?.revertCode);
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
}
|