Skip to content

Commit 618a6d7

Browse files
authored
feat(avm): seeding of fuzzer fee payer balance (#19089)
Adds function to fund the fee_payer account in cpp and TS before we start fuzzing the tx so that we can pay for the transaction fee for the tx. We use `maxFee` instead of `effectiveFee` so there is a slight overestimation.
1 parent d1f3efd commit 618a6d7

File tree

7 files changed

+39
-6
lines changed

7 files changed

+39
-6
lines changed

barretenberg/cpp/src/barretenberg/avm_fuzzer/common/interfaces/dbs.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55

66
#include "barretenberg/common/throw_or_abort.hpp"
77
#include "barretenberg/crypto/merkle_tree/indexed_tree/indexed_leaf.hpp"
8+
#include "barretenberg/crypto/poseidon2/poseidon2.hpp"
9+
#include "barretenberg/vm2/common/aztec_constants.hpp"
810
#include "barretenberg/vm2/common/aztec_types.hpp"
911

1012
using namespace bb::avm2::simulation;
13+
using Poseidon2 = bb::crypto::Poseidon2<bb::crypto::Poseidon2Bn254ScalarFieldParams>;
1114
using namespace bb::crypto::merkle_tree;
1215
using namespace bb::world_state;
1316

@@ -380,4 +383,18 @@ void FuzzerWorldStateManager::register_contract_address(const AztecAddress& cont
380383
ws->insert_indexed_leaves<NullifierLeafValue>(MerkleTreeId::NULLIFIER_TREE, { contract_nullifier }, fork_id);
381384
}
382385

386+
void FuzzerWorldStateManager::write_fee_payer_balance(const AztecAddress& fee_payer, const FF& balance)
387+
{
388+
if (fee_payer == 0) {
389+
return;
390+
}
391+
FF fee_juice_balance_slot = Poseidon2::hash({ FEE_JUICE_BALANCES_SLOT, fee_payer });
392+
FF leaf_slot =
393+
Poseidon2::hash({ GENERATOR_INDEX__PUBLIC_LEAF_INDEX, FF(FEE_JUICE_ADDRESS), fee_juice_balance_slot });
394+
395+
// Write to public data tree using current fork
396+
auto fork_id = fork_ids.top();
397+
ws->update_public_data(PublicDataLeafValue(leaf_slot, balance), fork_id);
398+
}
399+
383400
} // namespace bb::avm2::fuzzer

barretenberg/cpp/src/barretenberg/avm_fuzzer/common/interfaces/dbs.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class FuzzerWorldStateManager {
139139

140140
void reset_world_state();
141141
void register_contract_address(const AztecAddress& contract_address);
142+
void write_fee_payer_balance(const AztecAddress& fee_payer, const FF& balance);
142143

143144
world_state::WorldStateRevision get_current_revision() const;
144145
world_state::WorldStateRevision fork();

barretenberg/cpp/src/barretenberg/avm_fuzzer/fuzz_lib/constants.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ const FF SLOT_NUMBER = 1;
1818
const uint64_t TIMESTAMP = 1000000;
1919
const EthAddress COINBASE = EthAddress{ 0 };
2020
const AztecAddress FEE_RECIPIENT = AztecAddress{ 0 };
21-
const uint128_t FEE_PER_DA_GAS = 1;
22-
const uint128_t FEE_PER_L2_GAS = 1;
21+
constexpr uint128_t FEE_PER_DA_GAS = 1;
22+
constexpr uint128_t FEE_PER_L2_GAS = 1;
2323
const std::string TRANSACTION_HASH = "0xdeadbeef";
24-
const GasFees EFFECTIVE_GAS_FEES = GasFees{ .fee_per_da_gas = 0, .fee_per_l2_gas = 0 };
24+
constexpr GasFees EFFECTIVE_GAS_FEES = GasFees{ .fee_per_da_gas = FEE_PER_DA_GAS, .fee_per_l2_gas = FEE_PER_L2_GAS };
2525
const FF FIRST_NULLIFIER = FF("0x00000000000000000000000000000000000000000000000000000000deadbeef");
2626
const std::vector<FF> NON_REVERTIBLE_ACCUMULATED_DATA_NULLIFIERS = { FIRST_NULLIFIER };
2727
const std::vector<FF> NON_REVERTIBLE_ACCUMULATED_DATA_NOTE_HASHES = {};

barretenberg/cpp/src/barretenberg/avm_fuzzer/fuzz_lib/simulator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ SimulatorResult CppSimulator::simulate(fuzzer::FuzzerWorldStateManager& ws_mgr,
7575
{
7676

7777
const PublicSimulatorConfig config{
78-
.skip_fee_enforcement = true, // This is disabled once we need a prover fuzzer
78+
.skip_fee_enforcement = false,
7979
.collect_call_metadata = true,
8080
.collect_public_inputs = true,
8181
};

barretenberg/cpp/src/barretenberg/avm_fuzzer/tx.fuzzer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
3737
msgpack::unpack((reinterpret_cast<const char*>(data)), size).get().convert(tx_data);
3838
} catch (const std::exception& e) {
3939
fuzz_info("Failed to deserialize input in TestOneInput, creating default FuzzerTxData, exception: ", e.what());
40-
return 0;
40+
return -1; // Try rejecting inputs that cannot be deserialized
4141
}
4242

4343
FuzzerWorldStateManager* ws_mgr = FuzzerWorldStateManager::getInstance();

barretenberg/cpp/src/barretenberg/avm_fuzzer/tx_fuzzer_lib.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ SimulatorResult fuzz_tx(FuzzerTxData& tx_data)
4444
ws_mgr->register_contract_address(addr);
4545
}
4646

47+
// Compute fee from gas limits and max fees per gas (upper bound on fee)
48+
FF fee_required_da =
49+
FF(tx_data.tx.gas_settings.gas_limits.da_gas) * FF(tx_data.tx.gas_settings.max_fees_per_gas.fee_per_da_gas);
50+
FF fee_required_l2 =
51+
FF(tx_data.tx.gas_settings.gas_limits.l2_gas) * FF(tx_data.tx.gas_settings.max_fees_per_gas.fee_per_l2_gas);
52+
53+
ws_mgr->write_fee_payer_balance(tx_data.tx.fee_payer, fee_required_da + fee_required_l2);
54+
4755
// Run simulators
4856
auto cpp_simulator = CppSimulator();
4957
JsSimulator* js_simulator = JsSimulator::getInstance();

yarn-project/simulator/src/public/fuzzing/avm_fuzzer_simulator.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export class AvmFuzzerSimulator extends BaseAvmSimulationTester {
185185
super(contractDataSource, merkleTrees);
186186
const contractsDb = new PublicContractsDB(contractDataSource);
187187
this.simulator = new PublicTxSimulator(merkleTrees, contractsDb, globals, {
188-
skipFeeEnforcement: true,
188+
skipFeeEnforcement: false,
189189
collectDebugLogs: false,
190190
collectHints: false,
191191
collectStatistics: false,
@@ -209,6 +209,13 @@ export class AvmFuzzerSimulator extends BaseAvmSimulationTester {
209209
* Simulate a transaction from a C++ AvmTxHint.
210210
*/
211211
public async simulate(txHint: AvmTxHint): Promise<PublicTxResult> {
212+
// Compute fee from gas limits and max fees per gas (upper bound on fee)
213+
const totalFee =
214+
BigInt(txHint.gasSettings.gasLimits.daGas) * txHint.gasSettings.maxFeesPerGas.feePerDaGas +
215+
BigInt(txHint.gasSettings.gasLimits.l2Gas) * txHint.gasSettings.maxFeesPerGas.feePerL2Gas;
216+
217+
await this.setFeePayerBalance(txHint.feePayer, new Fr(totalFee));
218+
212219
const tx = await createTxFromHint(txHint);
213220
return await this.simulator.simulate(tx);
214221
}

0 commit comments

Comments
 (0)