Skip to content

Commit aa246c4

Browse files
committed
fixes
1 parent 57a633b commit aa246c4

File tree

13 files changed

+67
-26
lines changed

13 files changed

+67
-26
lines changed

barretenberg/cpp/src/barretenberg/vm2/common/avm_inputs.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ struct AvmProvingInputs {
395395
////////////////////////////////////////////////////////////////////////////
396396
struct TxSimulationResult {
397397
// Simulation.
398-
Gas gas_used;
398+
GasUsed gas_used;
399399
RevertCode revert_code;
400400
std::optional<SimulationError> revert_reason;
401401
std::optional<std::vector<FF>> app_logic_return_value;

barretenberg/cpp/src/barretenberg/vm2/common/aztec_types.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,16 @@ struct Gas {
209209
MSGPACK_FIELDS(l2Gas, daGas);
210210
};
211211

212+
struct GasUsed {
213+
Gas total_gas;
214+
Gas teardown_gas;
215+
Gas public_gas;
216+
Gas billed_gas;
217+
218+
bool operator==(const GasUsed& other) const = default;
219+
MSGPACK_CAMEL_CASE_FIELDS(total_gas, teardown_gas, public_gas, billed_gas);
220+
};
221+
212222
struct GasSettings {
213223
Gas gasLimits;
214224
Gas teardownGasLimits;

barretenberg/cpp/src/barretenberg/vm2/simulation/gadgets/tx_execution.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,13 @@ TxExecutionResult TxExecution::simulate(const Tx& tx)
235235
cleanup();
236236

237237
return {
238-
.gas_used = tx_context.gas_used,
238+
.gas_used = {
239+
.total_gas = tx_context.gas_used,
240+
// TODO(fcarreiro): complete.
241+
.teardown_gas = { 0, 0 },
242+
.public_gas = { 0, 0 },
243+
.billed_gas = { 0, 0 },
244+
},
239245
.revert_code = tx_context.revert_code,
240246
.app_logic_return_value = std::move(app_logic_return_value),
241247
};

barretenberg/cpp/src/barretenberg/vm2/simulation/gadgets/tx_execution.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace bb::avm2::simulation {
1818

1919
// TODO(fcarreiro): Create interface and move there.
2020
struct TxExecutionResult {
21-
Gas gas_used;
21+
GasUsed gas_used;
2222
RevertCode revert_code;
2323
std::optional<std::vector<FF>> app_logic_return_value;
2424
};

barretenberg/cpp/src/barretenberg/vm2/simulation_helper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ TxSimulationResult AvmSimulationHelper::simulate_fast(ContractDBInterface& raw_c
437437
// TODO(fcarreiro): get these values from somewhere.
438438
FF transaction_fee = 0;
439439
public_inputs_builder.extract_outputs(raw_merkle_db,
440-
tx_execution_result.gas_used,
440+
tx_execution_result.gas_used.total_gas,
441441
transaction_fee,
442442
tx_execution_result.revert_code != RevertCode::OK,
443443
side_effect_tracker.get_side_effects());

yarn-project/simulator/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"lodash.clonedeep": "^4.5.0",
7979
"lodash.merge": "^4.6.2",
8080
"tslib": "^2.4.0",
81-
"zod": "^4.1.12"
81+
"zod": "^3.23.8"
8282
},
8383
"devDependencies": {
8484
"@aztec/kv-store": "workspace:^",

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ export class CppPublicTxSimulatorHintedDbs extends PublicTxSimulator implements
8282
}
8383

8484
// Deserialize the msgpack result
85-
const cppResultJSON: any = deserializeFromMessagePack(resultBuffer);
86-
// TODO(fcarreiro): complete this.
87-
// console.log('cppResult', cppResultJSON);
88-
const cppResult = PublicTxResult.partialSchema.parse(cppResultJSON);
85+
const cppResultJSON: object = deserializeFromMessagePack(resultBuffer);
86+
const cppResult = PublicTxResult.fromJSON(cppResultJSON);
87+
8988
assert(cppResult.revertCode.equals(tsResult.revertCode));
89+
assert(cppResult.gasUsed.totalGas.equals(tsResult.gasUsed.totalGas));
9090

9191
this.log.debug(`C++ hinted simulation completed for tx ${txHash}`, {
9292
txHash,

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,49 @@ export class PublicTxResult {
4242
public publicInputs: AvmCircuitPublicInputs,
4343
) {}
4444

45-
static get schema() {
45+
/*
46+
static get schema(): ZodFor<PublicTxResult> {
4647
return z
4748
.object({
4849
gasUsed: schemas.GasUsed,
4950
revertCode: RevertCode.schema,
5051
revertReason: SimulationError.schema.optional(),
51-
appLogicReturnValue: z.array(schemas.Fr).optional(),
52-
logs: z.array(DebugLog.schema),
52+
appLogicReturnValue: Fr.schema.array().optional(),
53+
logs: DebugLog.schema.array(),
5354
hints: AvmExecutionHints.schema.optional(),
5455
publicInputs: AvmCircuitPublicInputs.schema,
5556
})
5657
.transform(
5758
({ gasUsed, revertCode, revertReason, appLogicReturnValue, logs, hints, publicInputs }) =>
58-
new PublicTxResult(gasUsed, revertCode, revertReason, appLogicReturnValue, logs, hints, publicInputs),
59+
new PublicTxResult(
60+
gasUsed,
61+
revertCode as RevertCode,
62+
revertReason,
63+
appLogicReturnValue,
64+
logs,
65+
hints,
66+
publicInputs,
67+
),
5968
);
60-
}
69+
}*/
6170

6271
// TODO(fcarreiro): complete this.
6372
static get partialSchema() {
6473
return z.object({
74+
gasUsed: schemas.GasUsed,
6575
revertCode: RevertCode.schema,
6676
});
6777
}
78+
static fromJSON(json: any) {
79+
console.log('json', json);
80+
// console.log('PI.globalVariables.gasFees', json.publicInputs.globalVariables.gasFees);
81+
const r = PublicTxResult.partialSchema.parse(json);
82+
console.log('after parse', r);
83+
return r as {
84+
gasUsed: GasUsed;
85+
revertCode: RevertCode;
86+
};
87+
}
6888
}
6989

7090
export type PublicTxSimulatorConfig = {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { readTestData, writeTestData } from '@aztec/foundation/testing/files';
44

55
import { makeAvmCircuitInputs } from '../tests/factories.js';
66
import { AvmCircuitInputs } from './avm.js';
7+
import { deserializeFromMessagePack } from './message_pack.js';
78

89
describe('Avm circuit inputs', () => {
910
// This tests that serde with the orchestrator works.
@@ -31,4 +32,14 @@ describe('Avm circuit inputs', () => {
3132
// generate the diff. This could otherwise take 10m+ and kill CI.
3233
expect(buffer.equals(expected)).toBe(true);
3334
});
35+
36+
// TODO(fcarreiro): fix this test.
37+
it.skip('serializes with MP and deserializes it back', async () => {
38+
const avmCircuitInputs = await makeAvmCircuitInputs(/*seed=*/ 0x1234);
39+
const buffer = avmCircuitInputs.serializeWithMessagePack();
40+
const json = deserializeFromMessagePack(buffer);
41+
// Parsing fails.
42+
const res = AvmCircuitInputs.schema.parse(json);
43+
expect(res).toStrictEqual(avmCircuitInputs);
44+
});
3445
});

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

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

21-
// This deserializes into a JSON-like object. If you want a specific
21+
// This deserializes into a JS object. If you want a specific
2222
// class, you need to use zod to parse it into the specific class.
2323
// You can use T.schema.parse() for that.
2424
export function deserializeFromMessagePack(buffer: Buffer): any {

0 commit comments

Comments
 (0)