Skip to content

Commit d0a1f50

Browse files
author
AztecBot
committed
Merge branch 'next' into merge-train/barretenberg
2 parents 8745960 + 6f47d59 commit d0a1f50

File tree

5 files changed

+41
-13
lines changed

5 files changed

+41
-13
lines changed

barretenberg/cpp/src/barretenberg/vm2/tracegen/execution_trace.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,12 @@ void ExecutionTraceBuilder::process(
605605
{ C::execution_enqueued_call_end, !has_parent ? 1 : 0 },
606606
{ C::execution_nested_exit_call, has_parent ? 1 : 0 },
607607
} });
608-
} else if (exec_opcode == ExecutionOpCode::GETENVVAR) {
608+
}
609+
// Separate if-statement for opcodes.
610+
// This cannot be an else-if chained to the above,
611+
// because `sel_exit_call` can happen on any opcode
612+
// and we still need to tracegen the opcode-specific logic.
613+
if (exec_opcode == ExecutionOpCode::GETENVVAR) {
609614
assert(ex_event.addressing_event.resolution_info.size() == 2 &&
610615
"GETENVVAR should have exactly two resolved operands (envvar enum and output)");
611616
// rop[1] is the envvar enum

barretenberg/cpp/src/barretenberg/vm2/tracegen/tx_trace.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ void TxTraceBuilder::process(const simulation::EventEmitterInterface<simulation:
529529

530530
// This is the tree state we will use during the "skipped" phases
531531
TxContextEvent propagated_state = startup_event_data.state;
532+
TxContextEvent end_setup_snapshot = startup_event_data.state;
532533
// Used to track the gas limit for the "padded" phases.
533534
Gas current_gas_limit = startup_event_data.gas_limit;
534535
Gas teardown_gas_limit = startup_event_data.teardown_gas_limit;
@@ -565,6 +566,10 @@ void TxTraceBuilder::process(const simulation::EventEmitterInterface<simulation:
565566
if (row == 1) {
566567
trace.set(row, handle_first_row());
567568
}
569+
if (phase == TransactionPhase::SETUP) {
570+
// If setup is empty, the end-setup-snapshot should just be current/propagated state
571+
end_setup_snapshot = propagated_state;
572+
}
568573
row++;
569574
continue;
570575
}
@@ -590,7 +595,6 @@ void TxTraceBuilder::process(const simulation::EventEmitterInterface<simulation:
590595
{ C::tx_start_phase, phase_counter == 0 ? 1 : 0 },
591596
{ C::tx_sel_read_phase_length, phase_counter == 0 && !is_one_shot_phase(tx_phase_event->phase) },
592597
{ C::tx_is_revertible, is_revertible(tx_phase_event->phase) ? 1 : 0 },
593-
594598
{ C::tx_end_phase, phase_counter == phase_events.size() - 1 ? 1 : 0 },
595599
} });
596600
trace.set(row, handle_prev_gas_used(gas_used));
@@ -654,6 +658,22 @@ void TxTraceBuilder::process(const simulation::EventEmitterInterface<simulation:
654658
}
655659
// In case we encounter another skip row
656660
propagated_state = phase_events.back()->state_after;
661+
662+
if (phase == TransactionPhase::SETUP) {
663+
// Store off the state at the end of setup to rollback to later on revert
664+
end_setup_snapshot = phase_events.back()->state_after;
665+
}
666+
if (phase_events.back()->reverted) {
667+
// On revert, roll back to end-setup-snapshot
668+
// Even though tx-execution events should already do this,
669+
// we need to update propagated state here so that any padded rows
670+
// get the correct rolled-back state rather then the pre-rollback state.
671+
propagated_state.tree_states = end_setup_snapshot.tree_states;
672+
propagated_state.written_public_data_slots_tree_snapshot =
673+
end_setup_snapshot.written_public_data_slots_tree_snapshot;
674+
propagated_state.side_effect_states = end_setup_snapshot.side_effect_states;
675+
// Note: we only rollback tree/side-effect states, not gas used or next_context_id.
676+
}
657677
}
658678
}
659679

yarn-project/simulator/src/public/avm/avm_simulator.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -710,9 +710,10 @@ describe('AVM simulator: transpiled Noir contracts', () => {
710710
expect(results.reverted).toBe(true);
711711
expect(results.revertReason?.message).toMatch(/Attempted to emit duplicate nullifier/);
712712

713-
// Nullifier should be traced exactly once
714-
expect(trace.traceNewNullifier).toHaveBeenCalledTimes(1);
715-
expect(trace.traceNewNullifier).toHaveBeenCalledWith(siloedNullifier0);
713+
// Nullifier should still be traced twice. Fails after second trace.
714+
expect(trace.traceNewNullifier).toHaveBeenCalledTimes(2);
715+
expect(trace.traceNewNullifier).toHaveBeenNthCalledWith(1, siloedNullifier0);
716+
expect(trace.traceNewNullifier).toHaveBeenNthCalledWith(2, siloedNullifier0);
716717
});
717718
});
718719

yarn-project/simulator/src/public/avm/opcodes/accrued_substate.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,9 @@ describe('Accrued Substate', () => {
204204
`Attempted to emit duplicate nullifier ${value0} (contract address: ${address}).`,
205205
),
206206
);
207-
expect(trace.traceNewNullifier).toHaveBeenCalledTimes(1);
208-
expect(trace.traceNewNullifier).toHaveBeenCalledWith(siloedNullifier0);
207+
expect(trace.traceNewNullifier).toHaveBeenCalledTimes(2);
208+
expect(trace.traceNewNullifier).toHaveBeenNthCalledWith(1, siloedNullifier0);
209+
expect(trace.traceNewNullifier).toHaveBeenNthCalledWith(2, siloedNullifier0);
209210
});
210211

211212
it('Nullifier collision reverts (nullifier exists in host state)', async () => {
@@ -216,7 +217,8 @@ describe('Accrued Substate', () => {
216217
`Attempted to emit duplicate nullifier ${value0} (contract address: ${address}).`,
217218
),
218219
);
219-
expect(trace.traceNewNullifier).toHaveBeenCalledTimes(0); // the only attempt should fail before tracing
220+
expect(trace.traceNewNullifier).toHaveBeenCalledTimes(1); // fails, but after tracing
221+
expect(trace.traceNewNullifier).toHaveBeenCalledWith(siloedNullifier0);
220222
});
221223
});
222224

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,15 @@ export class PublicPersistableStateManager {
138138
public async writeStorage(contractAddress: AztecAddress, slot: Fr, value: Fr, protocolWrite = false): Promise<void> {
139139
this.log.trace(`Storage write (address=${contractAddress}, slot=${slot}): value=${value}`);
140140

141+
await this.trace.tracePublicStorageWrite(contractAddress, slot, value, protocolWrite);
142+
141143
if (this.doMerkleOperations) {
142144
// write to native merkle trees
143145
await this.treesDB.storageWrite(contractAddress, slot, value);
144146
} else {
145147
// Cache storage writes for later reference/reads
146148
this.publicStorage.write(contractAddress, slot, value);
147149
}
148-
149-
await this.trace.tracePublicStorageWrite(contractAddress, slot, value, protocolWrite);
150150
}
151151

152152
public isStorageCold(contractAddress: AztecAddress, slot: Fr): boolean {
@@ -217,10 +217,10 @@ export class PublicPersistableStateManager {
217217
*/
218218
public async writeUniqueNoteHash(uniqueNoteHash: Fr): Promise<void> {
219219
this.log.trace(`noteHashes += @${uniqueNoteHash}.`);
220+
this.trace.traceNewNoteHash(uniqueNoteHash);
220221
if (this.doMerkleOperations) {
221222
await this.treesDB.writeNoteHash(uniqueNoteHash);
222223
}
223-
this.trace.traceNewNoteHash(uniqueNoteHash);
224224
}
225225

226226
/**
@@ -262,6 +262,8 @@ export class PublicPersistableStateManager {
262262
public async writeSiloedNullifier(siloedNullifier: Fr) {
263263
this.log.trace(`Inserting siloed nullifier=${siloedNullifier}`);
264264

265+
this.trace.traceNewNullifier(siloedNullifier);
266+
265267
if (this.doMerkleOperations) {
266268
const exists = await this.treesDB.checkNullifierExists(siloedNullifier);
267269

@@ -277,8 +279,6 @@ export class PublicPersistableStateManager {
277279
// Cache pending nullifiers for later access
278280
await this.nullifiers.append(siloedNullifier);
279281
}
280-
281-
this.trace.traceNewNullifier(siloedNullifier);
282282
}
283283

284284
/**

0 commit comments

Comments
 (0)