Skip to content

Commit f75cf2b

Browse files
committed
partially set up MP deser
1 parent 73352dc commit f75cf2b

File tree

5 files changed

+73
-31
lines changed

5 files changed

+73
-31
lines changed

yarn-project/simulator/src/public/public_processor/public_processor.test.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { strict as assert } from 'assert';
1616
import { type MockProxy, mock } from 'jest-mock-extended';
1717

1818
import { PublicContractsDB } from '../public_db_sources.js';
19-
import type { PublicTxResult, PublicTxSimulator } from '../public_tx_simulator/public_tx_simulator.js';
19+
import { PublicTxResult, type PublicTxSimulator } from '../public_tx_simulator/public_tx_simulator.js';
2020
import { GuardedMerkleTreeOperations } from './guarded_merkle_tree.js';
2121
import { PublicProcessor } from './public_processor.js';
2222

@@ -45,19 +45,15 @@ describe('public_processor', () => {
4545

4646
const stateReference = StateReference.empty();
4747

48-
mockedEnqueuedCallsResult = {
49-
hints: AvmExecutionHints.empty(),
50-
publicInputs: AvmCircuitPublicInputs.empty(),
51-
gasUsed: {
52-
totalGas: Gas.empty(),
53-
teardownGas: Gas.empty(),
54-
publicGas: Gas.empty(),
55-
billedGas: Gas.empty(),
56-
},
57-
revertCode: RevertCode.OK,
58-
appLogicReturnValue: [],
59-
logs: [],
60-
};
48+
mockedEnqueuedCallsResult = new PublicTxResult(
49+
/*gasUsed=*/ { totalGas: Gas.empty(), teardownGas: Gas.empty(), publicGas: Gas.empty(), billedGas: Gas.empty() },
50+
RevertCode.OK,
51+
undefined,
52+
/*appLogicReturnValue=*/ [],
53+
/*logs=*/ [],
54+
AvmExecutionHints.empty(),
55+
AvmCircuitPublicInputs.empty(),
56+
);
6157

6258
merkleTree.getPreviousValueIndex.mockResolvedValue({
6359
index: 0n,

yarn-project/simulator/src/public/public_tx_simulator/cpp_public_tx_simulator.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import { SimulationError } from '@aztec/stdlib/errors';
55
import type { MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
66
import type { GlobalVariables, Tx } from '@aztec/stdlib/tx';
77

8+
import { strict as assert } from 'assert';
9+
810
import type { ExecutorMetricsInterface } from '../executor_metrics_interface.js';
911
import type { PublicContractsDB } from '../public_db_sources.js';
10-
import { type PublicTxResult, PublicTxSimulator, type PublicTxSimulatorConfig } from './public_tx_simulator.js';
12+
import { PublicTxResult, PublicTxSimulator, type PublicTxSimulatorConfig } from './public_tx_simulator.js';
1113
import type {
1214
MeasuredPublicTxSimulatorInterface,
1315
PublicTxSimulatorInterface,
@@ -80,7 +82,11 @@ export class CppPublicTxSimulatorHintedDbs extends PublicTxSimulator implements
8082
}
8183

8284
// Deserialize the msgpack result
83-
const cppResult: PublicTxResult = deserializeFromMessagePack<PublicTxResult>(resultBuffer);
85+
const cppResultJSON: any = deserializeFromMessagePack(resultBuffer);
86+
// TODO(fcarreiro): complete this.
87+
// console.log('cppResult', cppResultJSON);
88+
const cppResult = PublicTxResult.partialSchema.parse(cppResultJSON);
89+
assert(cppResult.revertCode.equals(tsResult.revertCode));
8490

8591
this.log.debug(`C++ hinted simulation completed for tx ${txHash}`, {
8692
txHash,
@@ -89,7 +95,8 @@ export class CppPublicTxSimulatorHintedDbs extends PublicTxSimulator implements
8995
cppGasUsed: tsResult.gasUsed.totalGas.l2Gas,
9096
});
9197

92-
return cppResult;
98+
// TODO(fcarreiro): complete this.
99+
return tsResult;
93100
}
94101
}
95102

yarn-project/simulator/src/public/public_tx_simulator/public_tx_simulator.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ import { Fr } from '@aztec/foundation/fields';
33
import { type Logger, createLogger } from '@aztec/foundation/log';
44
import { ProtocolContractAddress, ProtocolContractsList } from '@aztec/protocol-contracts';
55
import { computeFeePayerBalanceStorageSlot } from '@aztec/protocol-contracts/fee-juice';
6-
import { AvmCircuitPublicInputs, AvmExecutionHints, AvmTxHint, type RevertCode } from '@aztec/stdlib/avm';
6+
import { AvmCircuitPublicInputs, AvmExecutionHints, AvmTxHint, RevertCode } from '@aztec/stdlib/avm';
77
import { SimulationError } from '@aztec/stdlib/errors';
88
import type { Gas, GasUsed } from '@aztec/stdlib/gas';
9-
import type { DebugLog } from '@aztec/stdlib/logs';
9+
import { DebugLog } from '@aztec/stdlib/logs';
10+
import { schemas } from '@aztec/stdlib/schemas';
1011
import type { MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
1112
import { type GlobalVariables, PublicCallRequestWithCalldata, Tx, TxExecutionPhase } from '@aztec/stdlib/tx';
1213

1314
import { strict as assert } from 'assert';
15+
import { z } from 'zod';
1416

1517
import type { AvmFinalizedCallResult } from '../avm/avm_contract_call_result.js';
1618
import { AvmSimulator } from '../avm/index.js';
@@ -27,17 +29,43 @@ import type { PublicPersistableStateManager } from '../state_manager/state_manag
2729
import { PublicTxContext } from './public_tx_context.js';
2830
import type { PublicTxSimulatorInterface } from './public_tx_simulator_interface.js';
2931

30-
export type PublicTxResult = {
31-
// Simulation result.
32-
gasUsed: GasUsed; // Gas used during the execution of this tx
33-
revertCode: RevertCode;
34-
revertReason?: SimulationError; // Revert reason, if any
35-
appLogicReturnValue?: Fr[]; // Return/revert value of the app logic phase
36-
logs: DebugLog[];
37-
// For the proving request.
38-
hints?: AvmExecutionHints;
39-
publicInputs: AvmCircuitPublicInputs;
40-
};
32+
export class PublicTxResult {
33+
constructor(
34+
// Simulation result.
35+
public gasUsed: GasUsed,
36+
public revertCode: RevertCode,
37+
public revertReason: SimulationError | undefined, // Revert reason, if any
38+
public appLogicReturnValue: Fr[] | undefined, // Return/revert value of the app logic phase
39+
public logs: DebugLog[],
40+
// For the proving request.
41+
public hints: AvmExecutionHints | undefined,
42+
public publicInputs: AvmCircuitPublicInputs,
43+
) {}
44+
45+
static get schema() {
46+
return z
47+
.object({
48+
gasUsed: schemas.GasUsed,
49+
revertCode: RevertCode.schema,
50+
revertReason: SimulationError.schema.optional(),
51+
appLogicReturnValue: z.array(schemas.Fr).optional(),
52+
logs: z.array(DebugLog.schema),
53+
hints: AvmExecutionHints.schema.optional(),
54+
publicInputs: AvmCircuitPublicInputs.schema,
55+
})
56+
.transform(
57+
({ gasUsed, revertCode, revertReason, appLogicReturnValue, logs, hints, publicInputs }) =>
58+
new PublicTxResult(gasUsed, revertCode, revertReason, appLogicReturnValue, logs, hints, publicInputs),
59+
);
60+
}
61+
62+
// TODO(fcarreiro): complete this.
63+
static get partialSchema() {
64+
return z.object({
65+
revertCode: RevertCode.schema,
66+
});
67+
}
68+
}
4169

4270
export type PublicTxSimulatorConfig = {
4371
proverId: Fr;

yarn-project/stdlib/src/avm/message_pack.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ export function serializeWithMessagePack(obj: any): Buffer {
1818
return encoder.encode(obj);
1919
}
2020

21-
export function deserializeFromMessagePack<T>(buffer: Buffer): T {
21+
// This deserializes into a JSON-like object. If you want a specific
22+
// class, you need to use zod to parse it into the specific class.
23+
// You can use T.schema.parse() for that.
24+
export function deserializeFromMessagePack(buffer: Buffer): any {
2225
setUpMessagePackExtensions();
2326
const decoder = new Decoder({
2427
useRecords: false,

yarn-project/stdlib/src/schemas/schemas.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { EventSelector } from '../abi/event_selector.js';
1010
import { FunctionSelector } from '../abi/function_selector.js';
1111
import { NoteSelector } from '../abi/note_selector.js';
1212
import { AztecAddress } from '../aztec-address/index.js';
13+
import { Gas } from '../gas/gas.js';
1314

1415
/**
1516
* Validation schemas for common types. Every schema must match its toJSON.
@@ -63,6 +64,13 @@ export const schemas = {
6364

6465
/** Accepts a hex string. */
6566
EventSelector: EventSelector.schema,
67+
68+
GasUsed: z.object({
69+
totalGas: Gas.schema,
70+
teardownGas: Gas.schema,
71+
publicGas: Gas.schema,
72+
billedGas: Gas.schema,
73+
}),
6674
};
6775

6876
export const AbiDecodedSchema: ZodFor<AbiDecoded> = z.union([

0 commit comments

Comments
 (0)