Skip to content

Commit 26f55a8

Browse files
committed
split up function selector and name in TS sim, cpp-ts sim comparison skips error message comparisons
1 parent d6d26f2 commit 26f55a8

File tree

14 files changed

+125
-49
lines changed

14 files changed

+125
-49
lines changed

barretenberg/cpp/src/barretenberg/nodejs_module/avm_simulate/avm_simulate_napi.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ struct ContractCallbacks {
101101
Napi::Value AvmSimulateNapi::simulate(const Napi::CallbackInfo& cb_info)
102102
{
103103
// TODO(dbanks12): configurable verbosity (maybe based on TS log level)
104-
verbose_logging = true;
104+
// verbose_logging = true;
105105
// debug_logging = true;
106106

107107
Napi::Env env = cb_info.Env();

barretenberg/cpp/src/barretenberg/vm2/simulation/lib/call_stack_metadata_collector.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,24 @@ ReturnDataProvider make_return_data_provider(const ContextInterface& context, ui
112112
try {
113113
const auto& memory = context.get_memory();
114114
std::vector<FF> data;
115+
info("Collecting returndata rd_offset:", rd_offset, " rd_size:", rd_size, " max_size:", max_size, ")");
115116
data.reserve(std::min(max_size, rd_size));
116117
for (uint32_t i = 0; i < std::min(max_size, rd_size); i++) {
117118
data.push_back(memory.get(rd_offset + i).as_ff());
118119
}
119120
return data;
120121
} catch (...) {
121-
vinfo("Failed to collect returndata (to:",
122-
context.get_address(),
123-
" pc:",
124-
context.get_pc(),
125-
" rd_offset:",
126-
rd_offset,
127-
" rd_size:",
128-
rd_size,
129-
" max_size:",
130-
max_size,
131-
")");
122+
info("Failed to collect returndata (to:",
123+
context.get_address(),
124+
" pc:",
125+
context.get_pc(),
126+
" rd_offset:",
127+
rd_offset,
128+
" rd_size:",
129+
rd_size,
130+
" max_size:",
131+
max_size,
132+
")");
132133
return {};
133134
}
134135
};

barretenberg/cpp/src/barretenberg/vm2/simulation/lib/call_stack_metadata_collector.test.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,15 +422,22 @@ TEST_F(MakeProviderTest, MakeReturnDataProviderSuccess)
422422
MemoryValue::from<FF>(FF(0x2222)),
423423
MemoryValue::from<FF>(FF(0x3333)),
424424
};
425+
auto zero = MemoryValue::from<FF>(FF(0));
425426

426427
auto provider = make_return_data_provider(mock_context, rd_addr, rd_size);
427428

428429
// This is called when invoking the provider
429430
StrictMock<MockMemory> memory;
430431
EXPECT_CALL(::testing::Const(mock_context), get_memory()).WillOnce(ReturnRef(memory));
431-
ON_CALL(memory, get).WillByDefault([&return_data_values](uint32_t i) -> const MemoryValue& {
432-
return return_data_values[i];
433-
});
432+
ON_CALL(memory, get)
433+
.WillByDefault([&return_data_values, rd_addr, rd_size, &zero](uint32_t i) -> const MemoryValue& {
434+
// if offset is not in range [rd_addr, rd_addr + rd_size], return 0
435+
if (i < rd_addr || i >= rd_addr + rd_size) {
436+
return zero;
437+
}
438+
const auto offset_into_rd = i - rd_addr;
439+
return return_data_values[offset_into_rd];
440+
});
434441
EXPECT_CALL(memory, get).Times(static_cast<int>(rd_size));
435442

436443
auto result = provider(1024);
-104 Bytes
Binary file not shown.

yarn-project/simulator/artifacts/avm_minimal_inputs.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,7 @@
9191
"commitment": "0x253955340ab6819b564797927db2433e18decc4b3f37cbd27dcf3b4d6222a694"
9292
}
9393
],
94-
"debugFunctionNames": [
95-
{
96-
"address": "0x03deefb26b3b88ef515e0ce5bd1402ea3a7c12569597cddb19ce6260444d625f",
97-
"selector": "0x000000000000000000000000000000000000000000000000000000000d6ab3ae",
98-
"name": "0x0d6ab3ae"
99-
}
100-
],
94+
"debugFunctionNames": [],
10195
"contractDbCreateCheckpointHints": [{ "actionCounter": 0, "oldCheckpointId": 0, "newCheckpointId": 1 }],
10296
"contractDbCommitCheckpointHints": [{ "actionCounter": 1, "oldCheckpointId": 1, "newCheckpointId": 0 }],
10397
"contractDbRevertCheckpointHints": [],

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export class AvmSimulator implements AvmSimulatorInterface {
177177

178178
if (machineState.pc >= bytecode.length) {
179179
this.log.warn('Passed end of program');
180-
throw new InvalidProgramCounterError(machineState.pc, /*max=*/ bytecode.length);
180+
throw new InvalidProgramCounterError(machineState.pc, /*max=*/ bytecode.length - 1);
181181
}
182182
}
183183

@@ -235,12 +235,15 @@ export class AvmSimulator implements AvmSimulatorInterface {
235235

236236
private async handleFailureToRetrieveBytecode(message: string): Promise<AvmContractCallResult> {
237237
// revert, consuming all gas
238-
const fnName = await this.context.persistableState.getPublicFunctionDebugName(this.context.environment);
238+
const { functionSelector, functionName } = await this.context.persistableState.getPublicFunctionSelectorAndName(
239+
this.context.environment,
240+
);
239241
const revertReason = new AvmRevertReason(
240242
message,
241243
/*failingFunction=*/ {
242244
contractAddress: this.context.environment.address,
243-
functionName: fnName,
245+
functionSelector,
246+
functionName,
244247
},
245248
/*noirCallStack=*/ [],
246249
);

yarn-project/simulator/src/public/avm/revert_reason.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ async function createRevertReason(message: string, revertData: Fr[], context: Av
1818
message = context.machineState.collectedRevertInfo.recursiveRevertReason.message;
1919
}
2020

21-
const fnName = await context.persistableState.getPublicFunctionDebugName(context.environment);
21+
const { functionSelector, functionName } = await context.persistableState.getPublicFunctionSelectorAndName(
22+
context.environment,
23+
);
2224

2325
return new AvmRevertReason(
2426
message,
2527
/*failingFunction=*/ {
2628
contractAddress: context.environment.address,
27-
functionName: fnName,
29+
functionSelector,
30+
functionName,
2831
},
2932
/*noirCallStack=*/ [...internalCallStack, context.machineState.pc].map(pc => `0.${pc}`),
3033
/*options=*/ { cause: nestedError },

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,28 @@ export async function getPublicFunctionDebugName(
1616
const selector = FunctionSelector.fromField(calldata[0]);
1717
return (await db.getDebugFunctionName(contractAddress, selector)) ?? selector.toString();
1818
}
19+
20+
/**
21+
* Get the function selector and optional debug name for a public function.
22+
* Returns the selector and name separately, with name only populated if a debug name is available.
23+
* @param db - The contracts database
24+
* @param contractAddress - The contract address
25+
* @param calldata - The calldata (selector is in calldata[0])
26+
* @returns An object with functionSelector (always if calldata[0] exists) and functionName (only if debug name found)
27+
*/
28+
export async function getPublicFunctionSelectorAndName(
29+
db: PublicContractsDBInterface,
30+
contractAddress: AztecAddress,
31+
calldata: Fr[],
32+
): Promise<{ functionSelector?: FunctionSelector; functionName?: string }> {
33+
// Public function is dispatched and therefore the target function is passed in the first argument.
34+
if (!calldata[0]) {
35+
return {};
36+
}
37+
const selector = FunctionSelector.fromField(calldata[0]);
38+
const debugName = await db.getDebugFunctionName(contractAddress, selector);
39+
return {
40+
functionSelector: selector,
41+
functionName: debugName ?? undefined,
42+
};
43+
}

yarn-project/simulator/src/public/fixtures/simple_contract_data_source.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,17 @@ export class SimpleContractDataSource implements ContractDataSource {
9090
return this.contractArtifacts.get(contractInstance!.currentContractClassId.toString());
9191
}
9292

93-
async getDebugFunctionName(address: AztecAddress, selector: FunctionSelector): Promise<string> {
93+
async getDebugFunctionName(address: AztecAddress, selector: FunctionSelector): Promise<string | undefined> {
9494
const contractInstance = await this.getContract(address);
9595
if (!contractInstance) {
96-
this.logger.warn(
97-
`Couldn't get fn name for debugging. Contract not in tester's ContractDataSource. Using selector:${selector} instead...`,
98-
);
99-
return `selector:${selector.toString()}`;
96+
this.logger.warn(`Couldn't get fn name for debugging. Contract not in tester's ContractDataSource.`);
97+
return undefined;
10098
}
10199
const key = `${contractInstance.currentContractClassId.toString()}:${selector.toString()}`;
102100
const fnName = this.debugFunctionName.get(key);
103101
if (!fnName) {
104-
this.logger.warn(`Couldn't get fn name for debugging. Using selector:${selector} instead...`);
105-
return selector.toString();
102+
this.logger.warn(`Couldn't get fn name for debugging...`);
103+
return undefined;
106104
}
107105
return fnName;
108106
}

0 commit comments

Comments
 (0)